Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0046 Jagged array

A jagged array is an array whose elements are arrays. Jagged array is an array of array. Each inner array dimension can be declared after the outter dimension has been initialized. The elements of a jagged array can be of different dimensions and sizes. The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers: int[][] jaggedArray = new int[3][]; Before you can use jaggedArray, its elements must be initialized. You can initialize the elements like this: jaggedArray[0] = new int[5]; jaggedArray[1] = new int[4]; jaggedArray[2] = new int[2]; Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers. It is also possible to use initializers to fill the array elements with values, in which case you do not need the array size. For example: jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 }; jaggedArray[1] = new int[] { 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 11, 22 }; You can also initialize the array upon declaration like this: int[][] jaggedArray2 = new int[][] { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} }; You can use the following shorthand form. Notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements: int[][] jaggedArray3 = { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} }; A jagged array elements are reference types and are initialized to null. You can access individual array elements like these examples: // Assign 7 to the second element ([1]) of the first array ([0]): jaggedArray3[0][1] = 7; // Assign 8 to the second element ([1]) of the third array ([2]): jaggedArray3[2][1] = 8; It is possible to mix jagged and multidimensional arrays. The following is a declaration and initialization of a single-dimensional jagged array that contains three two-dimensional array elements of different sizes. class MainClass { static void Main() { int[][,] jaggedArray4 = new int[3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {1,2}, {9,8}, {0,9} } }; } } You can access individual elements as shown in this example, which displays the value of the element [1,0] of the first array (value 5): class MainClass { static void Main() { int[][,] jaggedArray4 = new int[3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {1,2}, {9,8}, {0,9} } }; System.Console.Write("{0}", jaggedArray4[0][1, 0]); } } The output: 5 The method Length returns the number of arrays contained in the jagged array. class MainClass { static void Main() { int[][,] jaggedArray4 = new int[3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {1,2}, {9,8}, {0,9} } }; System.Console.WriteLine(jaggedArray4.Length); } } The output: 3 The following code declares a jagged array and fills the values and then uses nested for loop to output the array elements. class MainClass { static void Main() { // Declare the array of two elements: int[][] arr = new int[2][]; // Initialize the elements: arr[0] = new int[5] { 1, 3, 5, 7, 9 }; arr[1] = new int[4] { 2, 4, 6, 8 }; // Display the array elements: for (int i = 0; i < arr.Length; i++) { System.Console.Write("Element({0}): ", i); for (int j = 0; j < arr[i].Length; j++) { System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " "); } } } } The output: Element(0): 1 3 5 7 9Element(1): 2 4 6 8 The follwoing codes uses for loop to set the value of a jagged array. The lengh of each inner array is accessible by its own Length property. using System; class Program { static void Main(string[] args) { int[][] intArray = new int[3][]; for (int i = 0; i < intArray.Length; i++) { intArray[i] = new int[i + 2]; for (int j = 0; j < intArray[i].Length; j++) { intArray[i][j] = i + j; } } for (int i = 0; i < intArray.Length; i++) { for (int j = 0; j < intArray[i].Length; j++) { Console.Write(intArray[i][j]); } Console.WriteLine(); } } } The output: 01 123 2345