Java Reference
In-Depth Information
7.4.2. Arrays of Arrays
You can have arrays of arrays. The code to declare and print a two-di-
mensional matrix, for example, might look like this:
float[][] mat = new float[4][4];
setupMatrix(mat);
for (int y = 0; y < mat.length; y++) {
for (int x = 0; x < mat[y].length; x++)
System.out.print(mat[y][x] + " ");
System.out.println();
}
The first (left-most) dimension of an array must be specified when the
array is created. Other dimensions can be left unspecified, to be filled
in later. Specifying more than the first dimension is a shorthand for a
nested set of new statements. Our new creation could have been written
more explicitly as:
float[][] mat = new float[4][];
for (int y = 0; y < mat.length; y++)
mat[y] = new float[4];
One advantage of arrays of arrays is that each nested array can have a
different size. You can emulate a 4x4 matrix, but you can also create an
array of four int arrays, each of which has a different length sufficient
to hold its own data.
7.4.3. Array Initialization
When an array is created, each element is set to the default initial value
for its typezero for the numeric types, '\u0000' for char , false for boolean ,
 
Search WWH ::




Custom Search