Jagged Arrays
A 2-dimensional array has a rectangular size
(for example 3 by 3 elements). A jagged array is more flexible in
sizing the array. With a jagged array every row can have a
different size.
Figure 5-4
contrasts a 2-dimensional array that has 3x3 elements with a jagged
array. The jagged array shown contains three rows where the first
row has two elements, the second row has six elements, and the
third row has three elements.
A jagged array is declared by placing opening and
closing brackets after another. With the initialization of the
jagged array, first only the size that defines the number of rows
is set. The second brackets that define the number of elements
inside the row are kept empty as every row has a different number
of elements. Next the element number of the rows can be set for
every row:
Iterating through all elements of a jagged array
can be done with nested for loops. In
the outer for loop every row is
iterated, the inner for loop iterates
through every element inside a row.
The outcome of the iteration displays the rows and
every element within the rows: