Java Reference
In-Depth Information
because the embedded assignment that sets
i
to
1
is never executed.
Example 15.10.1-2. Multi-Dimensional Array Creation
The declaration:
float[][] matrix = new float[3][3];
is equivalent in behavior to:
float[][] matrix = new float[3][];
for (int
d
= 0;
d
< matrix.length;
d
++)
matrix[
d
] = new float[3];
and:
Age[][][][][] Aquarius = new Age[6][10][8][12][];
is equivalent to:
Age[][][][][] Aquarius = new Age[6][][][][];
for (int
d1
= 0;
d1
< Aquarius.length;
d1
++) {
Aquarius[
d1
] = new Age[10][][][];
for (int
d2
= 0;
d2
< Aquarius[
d1
].length;
d2
++) {
Aquarius[
d1
][
d2
] = new Age[8][][];
for (int
d3
= 0;
d3
< Aquarius[
d1
][
d2
].length;
d3
++) {
Aquarius[
d1
][
d2
][
d3
] = new Age[12][];
}
}
}
with
d
,
d1
,
d2
, and
d3
replaced by names that are not already locally declared. Thus,
a single
new
expression actually creates one array of length 6, 6 arrays of length 10,
6x10 = 60 arrays of length 8, and 6x10x8 = 480 arrays of length 12. This example
leaves the fifth dimension, which would be arrays containing the actual array ele-
ments (references to
Age
objects), initialized only to null references. These arrays can
be filled in later by other code, such as:
Age[] Hair = { new Age("quartz"), new Age("topaz") };
Aquarius[1][9][6][9] = Hair;
A triangular matrix may be created by:
float triang[][] = new float[100][];