Java Reference
In-Depth Information
flexibility in our programming by understanding more clearly the Java concept
of a multi-dimensional array. Actually, b contains the name of an array object of
length 2 , each element of which is the name of an array object of length 3 :
a0
a1
a2
1
4
8
b a0
5
6
2
a1
a2
Thus, b is array object a0, b[0] is row 0, which is array object a1, and b[1] is
row 1 , which is array object a2 . In this sense, there is no multi-dimensional array.
There is simply an array, b , each element of which is an array.
Variable b , of type int [][] , contains the name of a one-dimensional array.
Array elements b[0] and b[1] have type int [] .
Now the notation used earlier for referencing the lengths of rows and
columns makes sense:
b.length is the length of array object a0 : 2
b[0].length is the length of array object a1 : 3
b[1].length is the length of array object a2 : 3
9.3.1
Ragged arrays
Consider an array b that is declared and initialized like this:
int [][] c= new int [2][];
Array c is declared to be a two-dimensional array, but only the size of the first
dimension is given (but the brackets [] for the second dimension are there). Only
the elements in the first dimension are created, and they are set to null :
a6
c a6
null
null
Each element of c has type int [] , so arrays can be stored in them. Further,
the arrays in the elements can have different lengths! The assignments:
c[0]= new int [] {1, 3};
c[1]= new int [] {2, 4, 7 };
change c to look like this:
 
Search WWH ::




Custom Search