Information Technology Reference
In-Depth Information
Sub-Arrays in Jagged Arrays
Since the sub-arrays in a jagged array are themselves arrays, it is possible to have rectangular
arrays inside jagged arrays. For example, the following code creates a jagged array of three two-
dimensional rectangular arrays and initializes them with values. It then displays the values.
￿
The structure is illustrated in Figure 14-13.
The code also uses the GetLength(int n) method of arrays, inherited from System.Array ,
to get the length of the specified dimension of the array.
￿
int[][,] Arr; // An array of 2-D arrays
Arr = new int[3][,]; // Instantiate an array of three 2-D arrays.
Arr[0] = new int[,] { { 10, 20 }, { 100, 200 } };
Arr[1] = new int[,] { { 30, 40, 50 }, { 300, 400, 500 } };
Arr[2] = new int[,] { { 60, 70, 80, 90 }, { 600, 700, 800, 900 } };
Get length of dimension 0 of Arr
for (int i = 0; i < Arr.GetLength(0); i++)
{ Get length of dimension 0 of Arr[ i ]
for (int j = 0; j < Arr[i].GetLength(0); j++)
{ Get length of dimension 1 of Arr[ i ]
for (int k = 0; k < Arr[i].GetLength(1); k++) {
Console.WriteLine
("[{0}][{1},{2}] = {3}", i, j, k, Arr[i][j, k]);
}
Console.WriteLine("");
}
Console.WriteLine("");
}
Figure 14-13. Jagged array of three two-dimensional arrays
Search WWH ::




Custom Search