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, and null for reference types. When you declare an array of a
reference type, you are really declaring an array of variables of that type. Consider the following code:
Attr[] attrs = new Attr[12];
for (int i = 0; i < attrs.length; i++)
attrs[i] = new Attr(names[i], values[i]);
After the initial new of the array, attrs has a reference to an array of 12 variables that are initialized to
null. The Attr objects themselves are created only when the loop is executed.
You can initialize arrays with comma separated values inside braces following their declaration. The
following array declaration creates and initializes an array:
String[] dangers = { "Lions", "Tigers", "Bears" };
The following code gives the same result:
String[] dangers = new String[3];
dangers[0] = "Lions";
dangers[1] = "Tigers";
dangers[2] = "Bears";
When you initialize an array within its declaration, you don't have to explicitly create the array using newit is
done implicitly for you by the system. The length of the array to create is determined by the number of
initialization values given. You can use new explicitly if you prefer, but in that case you have to omit the