Page 431 - Introduction to Programming with Java: A Problem Solving Approach
P. 431

                For example, here’s a two-row by three-column array named x:
c
c
o
i
i
n
n
d
o
l
l
u
um
de
e
x
m
n
xe
n
10.9 Two-DimensionalArrays 397
 x
012
how to access each element
x[0][0] x[0][1] x[0][2] x[1][0] x[1][1] x[1][2]
        es
s
 8
 -2
 4
 1
 0
 5
0 1
    r
r
o
o
w
w
    i
in
n
d
d
e
ex
x
e
e
s
s
The items at the right, under the “how to access” column heading, show how to access each of the six ele- ments in the array. So to access the value 5, at row index 1 and column index 2, you specify x[1][2].
As with one-dimensional arrays, there are two ways to assign values into a two-dimensional array’s ele- ments. You can use an array initializer, where the element assignment is part of the array’s declaration. Or you can use standard assignment statements, where the assignment statements are separate from the array’s declaration and creation. We’ll describe the array initializer technique first. Here’s how you can declare the above two-dimensional x array and assign values into its elements, using an array initializer:
initializer for a 2-row by 3-column array
int[][] x = {{8,-2,4}, {1,0,5}};
Note that the array initializer contains two inner groups, where each inner group represents one row. {8, -2,4} represents the first row. {1,0,5} represents the second row. Note that elements and groups are
         separated with commas, and each inner group and the entire set of inner groups are surrounded by braces.
Apago PDF Enhancer
You can use the array initializer technique only if you know the assigned values when you first declare the array. Otherwise, you need to provide array element assignment statements that are separate from the array’s declaration and creation. For example, Figure 10.17’s code fragment declares and creates the x array in one statement, and assigns values to x’s elements in a separate statement, inside nested for loops.
  int[][] x = new int[2][3];
for (int i=0; i<x.length; i++)
{
for (int j=0; j<x[0].length; j++)
{
System.out.print("Enter value for row " + i + ", col " + j + ”: ”);
x[i][j] = stdIn.nextInt();
   } // end for j
} // end for i
Assign a value to the element at row i, column j.
Declare and create a 2-row by 3-column array.
 Figure 10.17 Assigning values into a two-dimensional array using nested for loops and the length property
When working with two-dimensional arrays, it’s very common to use nested for loops. In Figure 10.17, note the outer for loop with index variable i and the inner for loop with index variable j. The outer for loop iterates through each row, and the inner for loop iterates through each element within a particular row.
Figure 10.17’s first line declares x to be a 2-row by 3-column array with 6 total elements. So you might expect the first for loop’s x.length property to hold a 6. Not so. Even though it’s normal (and useful) to think of x as a rectangular box that holds 6 int elements, x is actually a reference to a
























   429   430   431   432   433