Java Reference
In-Depth Information
The indexes of each dimension in a multi-dimensional array are zero-based. Each element of the
table
array can
be accessed as
table[rownumber][columnNumber]
. The row number and the column number always starts at zero.
For example, you can assign a value to the first row and the second column in the
table
array as shown:
table[0][1] = 32;
You can assign a value 71 to the third row and the first column like so:
table[2][0] = 71;
The memory state after the above two assignments is depicted in Figure
15-8
.
table
0
32
0
0
71
0
X
Figure 15-8.
Memory state after two assignments to the two-dimensional array elements
Java does not support a multi-dimensional array in a true sense. Rather, it supports an array of arrays. Using an
array of arrays, you can implement the same functionality as provided by multi-dimensional arrays. When you create
a two-dimensional array, the elements of the first array are of an array type, which can refer to a single dimensional
array. The size of each single-dimensional array need not be the same. Considering the array of arrays concept for the
table
two-dimensional array, you can depict the memory state after array creation and assignments of two values as
shown in Figure
15-9
.
0
32
table
table[0]
X
table[1]
0
0
table[2]
71
0
Figure 15-9.
An array of arrays
The name of the two-dimensional array,
table
, refers to an array of three elements. Each element of the array
is a one-dimensional array of
int
. The data type of
table[0]
,
table[1]
and
table[2]
is an
int
array. The length of
table[0]
,
table[1]
and
table[2]
is each 2.