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]; |
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 |
Using two loops we can retrieve the values of the elements from all the arrays inside the jagged array:
// Loop through the 3 arrays |
If you compile the code, here’s the result in the console window:
10 |