One-dimensional arrays multidimensional arrays staggered array

 One-dimensional array:

A declaration of the array shown in Example 5 may be as follows integers:
int [] = the myArray new new int [. 5];
This array contains elements from myArray [0] to myArray [4] of. new operator to create an array and the array elements are initialized to their default values. In this embodiment, all of the array elements are initialized to zero.
It can be stored string element array declaration in the same manner. For example:
String [] = myStringArray new new String [. 6];
initialize the array
can be initialized in this case no level specifier array at declaration, as has been provided by the level specifier number of elements in the initialization list. For example:
int [] = the myArray new new int [] {. 1,. 3,. 5,. 7,. 9};
can be initialized with a string array in the same manner. The following statement an array of strings, wherein each array element is initialized with the name of the day:
String [] = WEEKDAYS new new String []
               { "the Sun", "Sat", "Mon", "Tue", "Wed", "Thu "," Fri "};
if the array in a statement to be initialized, you can use the following shortcut:
int [] the myArray = {. 1,. 3,. 5,. 7,. 9};
String [] WEEKDAYS = {" the Sun ", "Sat", "Mon", "Tue", "Wed", "Thu", "


= new new int the myArray [] {. 1,. 3,. 5,. 7,. 9}; // the OK
the myArray = {. 1,. 3,. 5,. 7,. 9}; // Error
value type and reference type array of arrays
, see the following array declaration :
MyType [] = new new MyType the myArray [10];
of the statement depends MyType value type or a reference type. If a value type, the statement creates an array of 10 instances MyType type thereof. If MyType is a reference type, the statement creates an array of 10 elements, where each element is initialized to null reference.
For more information on value types and reference types, see Type.
The array passed as a parameter
can be passed to the array initialization method. For example:
PrintArray (the myArray);
may be initialized and pass a new array in one step. For example:
PrintArray (new new int [] {. 1,. 3,. 5,. 7,. 9});
an example
, to initialize a string array and passes it to the following example PrintArray method as a parameter (element of the array shown in this method ):
// cs_sd_arrays.cs
the using the System;
public class ArrayClass
{
   static void PrintArray (String [] W)
   {
      for (int i = 0 ; i < w.Length ; i++)
         Console.Write(w[i] + "{0}", i < w.Length - 1 ? " " : "");
      Console.WriteLine();
   }

   public static void Main()
   {
      // Declare and initialize an array:
      string[] WeekDays = new string []
         {"Sun","Sat","Mon","Tue","Wed","Thu","Fri"};

      // Pass the array as a parameter:
      PrintArray(WeekDays);
   }
}
输出
Sun Sat Mon Tue Wed Thu Fri

 

 

Multidimensional Arrays:

 

You may have a plurality of array dimensions. For example, the following statement creates a two-dimensional array of four rows and two columns:
int [,] the myArray = new new int [4,2];
Further, the following statement creates a three-dimensional (4,2 and 3) the array:
int [,,] myArray = new int [4,2,3];
array initialization
may initialize an array declaration, the following example:
int [,] the myArray = new new int [,] {{1,2}, {3,4- }, {5,6}, {7,8}};
you may initialize the array without the specified level:
int [,] the myArray = {{1,2}, {3,4-}, {5,6}, { 7, 8}};
If you want to declare an array variable, but are not to be initialized, the operator must use the new array assigned to this variable. For example:
int [,] the myArray;
the myArray = new new int [,] {{1,2}, {3,4-}, {5,6}, {7, 8}}; // the OK
the myArray = {{. 1, 2}, {3,4}, { 5,6}, {7,8}}; // Error
may be assigned to an array element, for example:
the myArray [2,1] = 25;
an array as an argument
may be array initialization is passed to the method. For example:
PrintArray (the myArray);
may be initialized and pass a new array in one step. E.g:
PrintArray (new int [,] { {1,2}, {3,4}, {5,6}, {7,8}});
Example
In this example, a two-dimensional array initialized and passed to the PrintArray method (display elements of the array in this process).
Cs_td_arrays.cs //
the using the System;
public class ArrayClass
{
   static void PrintArray (int [,] W)
   {
      // Array Elements The Display:
      for (int I = 0; I <. 4; I ++)
         for (int J = 0; J <2; J ++)
            Console.WriteLine ( "the Element ({0}, {}. 1) = {2}", I, J, W [I, J]);
   }

   public static void Main()
   {
      // Pass the array as a parameter:
      PrintArray(new int[,] {{1,2}, {3,4}, {5,6}, {7,8}});
   }
}
输出
Element(0,0)=1
Element(0,1)=2
Element(1,0)=3
Element(1,1)=4
Element(2,0)=5
Element(2,1)=6
Element(3,0)=7
Element(3,1)=8

 

Staggered array:

 

Jagged array is an array of elements in the array. Dimensions and size of the staggered array elements can be different. Jagged arrays are sometimes referred to as "array of arrays." This topic contains the statement, the example of the array initialization and access staggered.

The following declaration of a one-dimensional array of three elements, where each element is a one-dimensional array of integers:

int[][] myJaggedArray = new int[3][];

You must initialize myJaggedArraythe elements before you can use it. : Initialization element illustrated embodiment may be as follows

myJaggedArray[0] = new int[5];
myJaggedArray[1] = new int[4];
myJaggedArray[2] = new int[2];

Each element is a one-dimensional array of integers. The first element is an array of five integers, the second is an array of four integers, and the third is an array of two integers.

May be used initializer fill the array element values, the array size is not required in this case, for example:

myJaggedArray[0] = new int[] {1,3,5,7,9};
myJaggedArray[1] = new int[] {0,2,4,6};
myJaggedArray[2] = new int[] {11,22};

You can also initialize the array at declaration, such as:

int[][] myJaggedArray = new int [][]
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};

Following shortcut can be used (note that the element can not be omitted in the initialization new operator, because there is no default initialization element):

int[][] myJaggedArray = {
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};

Individual array elements can be accessed as shown in the examples:

// Assign 33 to the second element of the first array:
myJaggedArray[0][1] = 33;
// Assign 44 to the second element of the third array:
myJaggedArray[2][1] = 44;

You can mix staggered arrays and multidimensional arrays. The following declare and initialize a one-dimensional staggered array, the array contains a two-dimensional array elements of different sizes:

int[][,] myJaggedArray = new int [3][,]
{
new int[,] { {1,3}, {5,7} },
new int[,] { {0,2}, {4,6}, {8,10} },
new int[,] { {11,22}, {99,88}, {0,9} }
};

Access the individual elements may be as follows, the example display element array of the first embodiment shown in [1,0]the value (value 5):

Console.Write("{0}", myJaggedArray[0][1,0]);

Examples

The following example to produce the array myArray, and the array element with the array. Each array element has a different size.

// cs_array_of_arrays.cs
using System;
public class ArrayTest
{
public static void Main()
{
// Declare the array of two elements:
int[][] myArray = new int[2][];
// Initialize the elements:
myArray[0] = new int[5] {1,3,5,7,9};
myArray[1] = new int[4] {2,4,6,8};
// Display the array elements:
for (int i=0; i < myArray.Length; i++)
{
Console.Write("Element({0}): ", i);
for (int j = 0 ; j < myArray[i].Length ; j++)
Console.Write("{0}{1}", myArray[i][j],
j == (myArray[i].Length-1) ? "" : " ");
Console.WriteLine();
}
}
}

Export

Element(0): 1 3 5 7 9
Element(1): 2 4 6 8


 

Reproduced in: https: //www.cnblogs.com/yitian/archive/2009/02/04/1383602.html

Guess you like

Origin blog.csdn.net/weixin_34129145/article/details/93710334