Java Reference
In-Depth Information
int[][] b = new int[5][];
Then allocate each row separately.
for (int i = 0; i < b.length; i++)
b[i] = new int[i + 1];
You can access each array element as b[i][j] , but be careful that j is less than
b[i].length .
Naturally, such Ȓraggedȓ arrays are not very common.
A DVANCED T OPIC 7.3: Multidimensional Arrays
You can declare arrays with more than two dimensions. For example, here is a
three-dimensional array:
int[][][] rubiksCube = new int[3][3][3];
Each array element is specified by three index values,
rubiksCube[i][j][k]
However, these arrays are quite rare, particularly in object-oriented programs, and
we will not consider them further.
310
311
7.7 Copying Arrays
Array variables work just like object variablesȌthey hold a reference to the actual
array. If you copy the reference, you get another reference to the same array (see
Figure 7 ):
An array variable stores a reference to the array. Copying the variable yields a
second reference to the same array.
double[] data = new double[10];
. . . // Fill array
double[] prices = data;
If you want to make a true copy of an array, call the clone method (see Figure 8 ).
Search WWH ::




Custom Search