Two-Dimensional Arrays
Objective #1: Declare and instantiate two-dimensional arrays.
- A two-dimensional array can be thought of as a numbers or text stored in a row-column format. You may think of an Excel spreadsheet with rows and columns storing numbers for a business. Two-dimensional arrays are sometimes called matrices (in math) or tables.
- The following statement declares & instantiates a two-dimensional array of integers named grades that has 3 rows and 4 columns.
int[][] grades = new int[3][4];
The first set of square brackets represents
the length of the one-dimensional array (i.e. the number of rows) and the second
set of square brackets represents the length of each of the rows (i.e. the
number of columns).
So the 2D array grades has 3 rows and 4 columns.
An alternate way to declare and instantiate a two-dimensional array is to
use an initializer list with curly braces as in
int[][] grades = {{88, 99, 100, 100}, {70, 85, 99, 91}, {85, 90, 23, 76}};
where each row is represented by a list of values in the inner sets of curly
braces and the outer set of curly braces lists the elements in the main one-dimensional
array.
- A two-dimensional array that a human draws like this
88 |
99 |
100 |
100 |
70 |
85 |
99 |
91 |
85 |
90 |
23 |
76 |
is really a one-dimensional array of arrays in the eyes of Java
- Notice that the grades.length is 3 since there are 3 rows in the 2D array grades
However since the element in index position 0 is another array {88, 99, 100, 100} then grades[0].length is 4 since {88, 99, 100, 100} has 4 elements.
For that matter grades[1].length is 4 since {70, 85, 99, 91} has 4 elements and grades[2].length is 4 since {85, 90, 23, 76} has 4 elements.
- It is important on the AP exam that you understand that a 2D array is an array of arrays.
Objective #2: Work with and traverse two-dimensional arrays.
- An assignment statement for a two-dimensional array is typed like this
grades[0][0] = 99;
where the top-left corner element of the two-dimensional array grades is being assigned
the value 99. Remember the rows and columns of a two-dimensional array are
numbered started at zero.
- Given the following two-dimensional array named grades, the following statements could be used:
grades |
0 |
1 |
2 |
3 |
0 |
88 |
99 |
100 |
100 |
1 |
70 |
85 |
99 |
91 |
2 |
85 |
90 |
23 |
76 |
System.out.println(grades[0][2]); // displays 100
System.out.println(grades[1][3]); // displays 91
System.out.println(grades[2][3]); // displays 76
System.out.println(grades[1][0] + grades[0][1]); // displays 169 since 70 + 99 = 169
System.out.println(grades[2][50]); // causes ArrayIndexOutOfBoundsException error
System.out.println(grades[3][2]); // causes ArrayIndexOutOfBoundsException error
The columns are numbered in red and the rows are numbered in blue.
- A double-nested for loop is often used to iterate through a two-dimensional
array searching for a particular key value as in
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (grades[row][col] == 100)
{
System.out.println("I found
a 100");
}
}
}
- The length property of arrays allows you to access the
number of rows in a two-dimensional array. The statement System.out.println(grades.length)); would display 3 since the "outer" one dimensional array has 3 rows. The statement System.out.println(grades[0].length); would display 4 since the length of
the one-dimensional array that is stored in position 0 of the "outer" array
is 4. In other words, the number of columns in the two-dimensional array
grades is 4. Therefore the code segment above could be rewritten to take
advantage of the length property:
for (int row = 0; row < grades.length; row++)
{
for (int col = 0; col < grades[row].length; col++)
{
if (grades[row][col] == 100)
{
System.out.println("I
found
a 100");
}
}
}
- While Java does allow a two-dimensional array to have rows of varying lengths
(called a ragged array or partially-filled array), the AP exam only covers
rectangular two-dimensional arrays where all rows have the same number of
elements.
- In the following example, a for each loop is being used to iterate through each row of a two-dimensional array. Notice the use of the toString method from the Arrays class. It is extremely likely that you will have to use a for each loop with a two-dimensional array on the AP exam in 2014-15 and beyond!
int[][] table = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
for (int[] row : table)
{
System.out.println(Arrays.toString(row));
}
This algorithm makes use of the toString method from the Arrays class to neatly print the rows of an two-dimensional array.
- The following example demonstrates how double-nested for each loops can be used to traverse a two-dimensional array
for (int[] row : matrix)
for (int num: row)
doSomething...