Common Multidimensional-Array Manipulations Performed with for Statements
Many common array manipulations use for
statements. As an example, the following for
statement sets all the elements in row 2
of array a
in Fig. 7.16 to zero:
for (int column = 0; column < a[2].length; column++) a[2][column] = 0;
We specified row 2
; therefore, we know that the first index is always 2
(0
is the first row, and 1
is the second row). This for
loop varies only the second index (i.e., the column index). If row 2
of array a
contains four elements, then the preceding for
statement is equivalent to the assignment statements
a[2][0] = 0;a[2][1] = 0;a[2][2] = 0;a[2][3] = 0;
The following nested for ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.