Information Technology Reference
In-Depth Information
Declaring a Jagged Array
The declaration syntax for jagged arrays requires a separate set of square brackets for each
dimension. The number of sets of square brackets in the declaration of the array variable deter-
mines the rank of the array.
A jagged array can be of any number of dimensions greater than one.
As with rectangular arrays, dimension lengths cannot be included in the array type sec-
tion of the declaration.
Rank specifiers
↓
int[][] SomeArr; // Rank = 2
int[][][]
OtherArr; // Rank = 3
↑
↑
Array type
Array name
Shortcut Instantiation
You can combine the jagged array declaration with the creation of the first-level array using
an array creation expression, such as in the following declaration. The result is shown in
Figure 14-11.
Three sub-arrays
↓
int[][] jagArr = new int[3][];
Figure 14-11.
Shortcut first-level instantiation
You cannot instantiate more than the first-level array in the declaration statement.
Allowed
↓
int[][] jagArr = new int[3][4]; // Wrong! Compile error
↑
Not allowed