Java Reference
In-Depth Information
The statement:
sales[5][3] = 25.75;
stores
25.75
into row number
5
and column number
3
(the
6th
row and the
4th
column) of the array
sales
(see Figure 9-17).
sales
[0] [1] [2] [3] [4]
[0]
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0
sales[5][3]
[1]
[2]
[3]
[4]
[5]
[6]
0.0 0.0 0.0
25.75
0.0
0.0 0.0 0.0
0.0
0.0
[7]
0.0 0.0 0.0
0.0
0.0
[8]
0.0 0.0 0.0
0.0
0.0
[9]
0.0 0.0 0.0
0.0
0.0
FIGURE 9-17
sales[5][3]
Suppose that:
int
i = 5;
int
j = 3;
Then, the previous statement:
sales[5][3] = 25.75;
is equivalent to:
sales[i][j] = 25.75;
So the indices can also be variables.
TWO-DIMENSIONAL ARRAYS AND THE INSTANCE VARIABLE
length
Just as in one-dimensional arrays, you can use the instance variable
length
to determine
the number of rows as well as the number of columns (in each row). Consider the
following statement:
int
[][] matrix =
new int
[20][15];
This statement declares and instantiates a two-dimensional array
matrix
of
20
rows and
15
columns. The value of the expression:
matrix.length
is
20
, the number of rows.
Search WWH ::
Custom Search