c sharp array applies analysis

  • 2020-05-05 11:49:03
  • OfStack

The following is from MSDN

An array is an ordered collection of items of the same data type. To access an item in an array, you use both the array name and the offset between the item and the starting point of the array. In   C#  , the way arrays are declared and used differs significantly from   Java  .  

One dimensional array  
A one-dimensional array stores a fixed number of items in a linear fashion, requiring only an index value to identify any item. In   C#  , the square brackets in the array declaration must follow the data type and cannot be placed after the variable name, which is allowed in   Java  . Therefore, arrays of type   integers   should be declared using the following syntax:  

int[]   arr1;  
The following declaration is invalid in   C#  :  

//int   arr2[];   //compile   error  
Once the array is declared, you can set its size using the   new   keyword, which is the same as   Java  . The following code declares an array reference:  

int[]   arr;  
arr   =   new   int[5];   //   create   a   5   element   integer   array  
You can then access elements in a one-dimensional array using the same syntax as   Java  . The C#   array index also starts from zero. The following code accesses the last element in the above array:  

System.Console.WriteLine(arr[4]);   //   access   the   5th   element  
Initialize  

C#   array elements can be initialized at creation using the same syntax as   Java  :  

int[]   arr2Lines;  
arr2Lines   =   new   int[5]   {1,   2,   3,   4,   5};  
However, the number of initializers for   C#   must exactly match the array size, unlike   Java  . You can use this to declare and initialize the   C#   array on the same line:  

int[]   arr1Line   =   {1,   2,   3,   4,   5};  
This syntax creates an array whose size is equal to the number of initializers.  

Initialize  
in the program loop Another way to initialize an array in   C#   is to use an   for   loop. The following loop sets each element of the array to zero:  

int[]   TaxRates   =   new   int[5];  

for   (int   i=0;   i < TaxRates.Length;   i++)  
{  
TaxRates[i]   =   0;  
}  
The jagged array  
Both C#   and   Java   support the creation of staggered (non-rectangular) arrays, that is, arrays with a different number of columns per row. For example, in the following jagged array, the first row has four entries, and the second row has three:  

int[][]   jaggedArray   =   new   int[2][];  
jaggedArray[0]   =   new   int[4];  
jaggedArray[1]   =   new   int[3];  
The multidimensional array  
You can use   C#   to create regular multidimensional arrays that are similar to matrices of the same type of value. While both   Java   and   C#   support jagging arrays,   C#   also supports multidimensional arrays (arrays of arrays).  

Declare an array of multidimensional rectangles using the following syntax:  

int[,]   arr2D;   //   declare   the   array   reference  
float[,,,]   arr4D;   //   declare   the   array   reference  
After the declaration, you can allocate memory to the array as follows:  

arr2D   =   new   int[5,4];   //   allocate   space   for   5   x   4   integers  
The elements of the array can then be accessed using the following syntax:  

arr2D[4,3]   =   906;  
Since the array starts from zero, this row sets the element in the fourth row and fifth column to   906.  

Initialize  
You can create, set, and initialize a multidimensional array in the same statement using one of the following methods:  

int[,]   arr4   =   new   int   [2,3]   {   {1,2,3},   {4,5,6}   };  
int[,]   arr5   =   new   int   [,]   {   {1,2,3},   {4,5,6}   };  
int[,]   arr6   =   {   {1,2,3},   {4,5,6}   };  

Initialize  
in the program loop You can initialize all the elements in the array using the nested loop shown here:  

int[,]   arr7   =   new   int[5,4];  

for(int   i=0;   i < 5;   i++)  
{  
for(int   j=0;   i < 4;   j++)  
{  
arr7[i,j]   =   0;   //   initialize   each   element   to   zero  
}  
}  
System.Array    
In  .NET   Framework  , arrays are implemented as instances of the   Array   class. This class provides many useful methods, such as   Sort   and   Reverse.  

The following example demonstrates how easy it is to use these methods. First, invert the array elements using the   Reverse   method, then sort them using the   Sort   method:  

class   ArrayMethods  
{  
static   void   Main()  
{  
//   Create   a   string   array   of   size   5:  
string[]   employeeNames   =   new   string[5];  

//   Read   5   employee   names   from   user:  
System.Console.WriteLine("Enter   five   employee   names:");  
for(int   i=0;   i < employeeNames.Length;   i++)  
{  
employeeNames[i]=   System.Console.ReadLine();  
}  

//   Print   the   array   in   original   order:  
System.Console.WriteLine("\nArray   in   Original   Order:");  
foreach(string   employeeName   in   employeeNames)  
{  
System.Console.Write("{0}   ",   employeeName);  
}  

//   Reverse   the   array:  
System.Array.Reverse(employeeNames);  

//   Print   the   array   in   reverse   order:  
System.Console.WriteLine("\n\nArray   in   Reverse   Order:");  
foreach(string   employeeName   in   employeeNames)  
{  
System.Console.Write("{0}   ",   employeeName);  
}  

//   Sort   the   array:  
System.Array.Sort(employeeNames);  

//   Print   the   array   in   sorted   order:  
System.Console.WriteLine("\n\nArray   in   Sorted   Order:");  
foreach(string   employeeName   in   employeeNames)  
{  
System.Console.Write("{0}   ",   employeeName);  
}  
}  
}  
Output  
Enter   five   employee   names:  

Luca  

Angie  

Brian  

Kent  

Beatriz  


Array   in   Original   Order:  

Luca   Angie   Brian   Kent   Beatriz  


Array   in   Reverse   Order:  

Beatriz   Kent   Brian   Angie   Luca  


Array   in   Sorted   Order:  

Angie   Beatriz   Brian   Kent   Luca

Related articles: