Java Reference
In-Depth Information
TwoDimArraysMethods.largestInRows(board);
//Line 10
} //end main
//Line 11
}
//Line 12
Sample Run:
23
5
6
15
18
4
16
24
67
10
12
54
23
76
11
1
12
34
22
8
81
54
32
67
33
12
34
76
78
9
The sum of the elements of row 1 = 67.
The sum of the elements of row 2 = 121.
The sum of the elements of row 3 = 176.
The sum of the elements of row 4 = 77.
The sum of the elements of row 5 = 267.
The sum of the elements of row 6 = 209.
The largest element of row 1 = 23.
The largest element of row 2 = 67.
The largest element of row 3 = 76.
The largest element of row 4 = 34.
The largest element of row 5 = 81.
The largest element of row 6 = 78.
In the preceding program, the statement in Line 5 declares and initializes board to be a
two-dimensional array of 6 rows and 5 columns. The statement in Line 6 uses the
method printMatrix to output the elements of board (see the first six lines of the
Sample Run). The statement in Line 8 uses the method sumRows to calculate and print
the sum of each row. The statement in Line 10 uses the method largestInRows to find
and print the largest element in each row.
9
When storing a two-dimensional array in computer memory, Java uses the row order
form. That is, the first row is stored first, followed by the second row, followed by the
third row, and so on.
Multidimensional Arrays
Earlier in this chapter, we defined an array as a collection of a fixed number of variables
called elements or components of the same type. A one-dimensional array is an array in
which the elements are arranged in a list form; in a two-dimensional array, the elements
are arranged in a table form. We can also define three-dimensional or larger arrays. In
Java, there is no limit on the dimensions of arrays. The following is the general definition
of an array:
n -dimensional array: A collection of a fixed number of variables, called elements or
components, arranged in n dimensions (n 1).
 
 
Search WWH ::




Custom Search