Professional Documents
Culture Documents
CMPS161ClassNotesChap07 PDF
CMPS161ClassNotesChap07 PDF
Multidimensional Arrays
7.1 Introduction
Thus far, you have used one-dimensional arrays to model linear collections of elements. You
can use a two-dimensional array to represent a matrix or a table. For example, the following
table that describes the distances between the cities can be represented using a two-
dimensional array.
You can create a two-dimensional array of 5 by 5 int values and assign it to matrix using this
syntax:
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
FIGURE 7.1 The index of each subscript of a multidimensional array is an int value starting
from 0.
Caution
It is a common mistake to use matrix[2,1] to access the element at row 2 and column 1.
In Java, each subscript must be enclosed in a pair of square brackets.
You can also use an array initializer to declare, create and initialize a two-dimensional array.
For example,
x
x[0][0] x[0][1] x[0][2] x[0][3] x[0].length is 4
x[0]
x[1] x[1][0] x[1][1] x[1][2] x[1][3] x[1].length is 4
x[2]
x[2][0] x[2][1] x[2][2] x[2][3] x[2].length is 4
x.length is 3
Each row in a two-dimensional array is itself an array. Thus, the rows can have different
lengths.
int[][] triangleArray = { 1 2 3 4 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, 1 2 3 4
{3, 4, 5},
{4, 5}, 1 2 3
{5}
}; 1 2
1 2
If you don’t know the values in a raged array in advance, but know the sizes, say the same as
before, you can create a ragged array using the syntax that follows:
o (Initializing arrays with random values) You can now assign random values to the array
using the following loop:
o (Printing arrays)
You can pass a two-dimensional array to a method just as you pass a one-dimensional array.
Listing 7.1 gives an example with a method that returns the sum of all the elements in a
matrix.
import java.util.Scanner;
// Display result
System.out.println("\nSum of all elements is " + sum(m));
}
return total;
}
}
import java.util.Scanner;
// Display result
System.out.println("The closest two points are " +
"(" + points[p1][0] + ", " + points[p1][1] + ") and (" +
points[p2][0] + ", " + points[p2][1] + ")");
}
/** Compute the distance between two points (x1, y1) and (x2, y2)*/
public static double distance(
double x1, double y1, double x2, double y2) {
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
}
This section presents an interesting problem of a sort that appears in the newspaper every
day: Sudoku
Sudoku is a 9 X 9 grid divided into smaller 3 X 3 boxes (also called regions or blocks) as
shown in Figure 7.4(a).
Some cells, called fixed cells, are populated with numbers from 1 to 9.
The objective is to fill the empty cells, also called free cells, with numbers 1 to 9 so that
every row, every column, and every 3 X 3 box contains the numbers 1 to 9 as shown in
Figure 7.4 (b).
5 3 7 5 3 4 6 7 8 9 1 2
6 1 9 5 6 7 2 1 9 5 3 4 8
9 8 6 1 9 8 3 4 2 5 6 7
8 6 3 8 5 9 7 6 1 4 2 3
4 8 3 1 4 2 6 8 5 3 7 9 1
7 2 6 7 1 3 9 2 4 8 5 6
6 9 6 1 5 3 7 2 8 4
4 1 9 5 2 8 7 4 1 9 6 3 5
8 7 9 3 4 5 2 8 6 1 7 9
import java.util.Scanner;
return grid;
}
The following syntax declares a three-dimensional array variable scores, creates an array, and
assigns its reference to scores:
double [ ] [ ] [ ] x = new double[2][3][4];
1 3 5 7 2 3 6 7 4 5 6 7 8 9 10 11 16 17 18 19
9 11 13 15 10 11 14 15 12 13 14 15 12 13 14 15 20 21 22 23
17 19 21 23 18 19 22 23 20 21 22 23 24 25 26 27 24 25 26 27
25 27 29 31 26 27 30 31 28 29 30 31 28 29 30 31 28 29 30 31
Set1 Set2 Set3 Set4 Set5
19 7 23
import java.util.Scanner;
int[][][] dates = {
{{ 1, 3, 5, 7},
{ 9, 11, 13, 15},
{17, 19, 21, 23},
{25, 27, 29, 31}},
{{ 2, 3, 6, 7},
{10, 11, 14, 15},
{18, 19, 22, 23},
{26, 27, 30, 31}},
{{ 4, 5, 6, 7},
{12, 13, 14, 15},
{20, 21, 22, 23},
{28, 29, 30, 31}},
{{ 8, 9, 10, 11},
{12, 13, 14, 15},
{24, 25, 26, 27},
{28, 29, 30, 31}},
{{16, 17, 18, 19},
{20, 21, 22, 23},
{24, 25, 26, 27},
{28, 29, 30, 31}}};
if (answer == 1)
day += dates[i][0][0];
}