EXPRESSIONS
Run time Evaluation of Array Creation Expressions
15.9.1
Then, if a single
DimExpr
appears, a single dimensional array is created of
the specified length, and each component of the array is initialized to its standard
default value ( 4.5.4).
If an array creation expression contains
N DimExpr
expressions, then it effec
tively executes a set of nested loops of depth
N
1 to create the implied arrays of
arrays. For example, 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[8][][][];
for (int d2 = 0; d2 < Aquarius[d1].length; d2++) {
Aquarius[d1][d2] = new Age[10][][];
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, 6
10 = 60 arrays of length 8, and 6
10
8 = 480 arrays of length
12. This example leaves the fifth dimension, which would be arrays containing the
actual array elements (references to
Age
objects), initialized only to null refer
ences. 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 multidimensional array need not have arrays of the same length at each
level; thus, a triangular matrix may be created by:
float triang[][] = new float[100][];
for (int i = 0; i < triang.length; i++)
triang[i] = new float[i+1];
There is, however, no way to get this effect with a single creation expression.
317
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved