Jagged arrays

This tutorial explains what a jagged array by giving an example that shows you how set and retrieve values.

They say a jagged array is an array of arrays.

It’s not so hard to comprehend this. In the following example we declare jaggedArray named jArray that can hold 3 other arrays:

int[][] jArray = new int[3][];

Next you can set the size (number of elements) of each of the 3 arrays:

jArray[0] = new int[2];

jArray[1] = new int[5];

jArray[2] = new int[7];

The first array (0) has 2 elements, the second (1) has 5 elements and the third (2) has 7 elements.

How do we assign and retrieve values from the elements of each array?

As you can see in the followin graphic, first we specify the number of the array, second we specify the number of the element:

In the following example we assign values to all the elements in all 3 arrays:

// Fill the array no. 0

jArray[0][0] = 10;

jArray[0][1] = 11;

// Fill the array no. 1

jArray[1][0] = 20;

jArray[1][1] = 21;

jArray[1][2] = 22;

jArray[1][3] = 23;

jArray[1][4] = 24;

// Fill the array no. 2

jArray[2][0] = 30;

jArray[2][1] = 31;

jArray[2][2] = 32;

jArray[2][3] = 33;

jArray[2][4] = 34;

jArray[2][5] = 35;

jArray[2][6] = 36;

Using two loops we can retrieve the values of the elements from all the arrays inside the jagged array:

// Loop through the 3 arrays

for(int i = 0; i < jArray.GetLength(0); i++)


   // Loop through the elements of the current array

   for(int x = 0; x < jArray[i].GetLength(0); x++)






If you compile the code, here’s the result in the console window:


















Press any key to continue
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top