English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Jagged Arrays

Jagged arrays are arrays of arrays. Jagged arrays store arrays rather than literals. For example, a two-dimensional array is a one-dimensional array of multiple elements of the same type, but a jagged array, the number of elements in each array below can be different.

Jagged arrays are initialized with two square brackets [] [], the first bracket specifies the size of the array, and the second bracket specifies the size of the array to be stored.

The following example declares a jagged array.

int[][] jArray1 = new int[2]; // can contain two one-dimensional arrays 
int[][,] jArray2 = new int[3][,]; // can contain three two-dimensional arrays

In the above example, jArray1You can store up to two one-dimensional arrays. jArray2You can store up to three two-dimensional arrays, the array [ ] specifies the two-dimensional array.

int[][] jArray = new int[2]; 
jArray[0] = new int[3]{1, 2, 3};
jArray[1] = new int[4]{4, 5, 6, 7 };

You can also initialize a jagged array at declaration, as shown below.

int[][] jArray = new int[2]{
                new int[3]{1, 2, 3},
                new int[4]{4, 5, 6, 7}
            };
jArray[0][0]; //Return1
jArray[0][1]; //Return2
jArray[0][2]; //Return3
jArray[1][0]; //Return4
jArray[1][}}1]; //Return5
jArray[1][}}2]; //Return6
jArray[1][}}3]; //Return7

You can use two for loops to access a jagged array, as shown below.

int[][] jArray = new int[2]{
                new int[3]{1, 2, 3},
                new int[4]{4, 5, 6, 7}
            };
for(int i = 0; i < jArray.Length;++)
{
	for(int j = 0; j < (jArray[i]).Length;++)
		Console.WriteLine(jArray[i][j]);
}

The following jagged array stores two-dimensional arrays, where the second bracket [ ] represents a two-dimensional array.

int[][,] jArray = new int[2][,];
jArray[0] = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }
jArray[1] = new int[2, 2] { { 7, 8 }, { 9, 10 } } 
jArray[0][1, 1]; //Return4
                                             
jArray[1][}}1, 0]; //Return9
                                             
jArray[1][}}1, 1]; //Return10

If you add another bracket, then it is an array of arrays.

int[][][] intJaggedArray = new int[2]] 
                            {
                                new int[2}][]  
                                { 
                                    new int[3] { 1, 2, 3},
                                    new int[2] { 4, 5} 
                                },
                                new int[1}][]
                                { 
                                    new int[3] { 7, 8, 9}
                                }
                            };
Console.WriteLine(intJaggedArray[0][0][0]); // 1
Console.WriteLine(intJaggedArray[0][1][}}1]); // 5
    
Console.WriteLine(intJaggedArray[1][0][2]); // 9

In the above jagged array example, three square brackets [][][] represent an array of arrays. Therefore, intJaggedArray will contain two elements, meaning two arrays. Now, each array still contains an array (one-dimensional). intJaggedArray[0][0][0] points to the first element of the first internal array. intJaggedArray[1][0][2]] pointing to the third element of the second internal array. The following figure illustrates this.

Jagged Arrays