Java Reference
In-Depth Information
With this observation, you can easily identify all the cells in the box. For instance, if
grid[r][c] is the starting cell of a
3
*
3
box, the cells in the box can be traversed in a
nested loop as follows:
// Get all cells in a 3-by-3 box starting at grid[r][c]
for ( int row = r; row < r + 3 ; row++)
for ( int col = c; col < c + 3 ; col++)
// grid[row][col] is in the box
It is cumbersome to enter 81 numbers from the console. When you test the program, you may
store the input in a file, say CheckSudokuSolution.txt (see www.cs.armstrong.edu/liang/data/
CheckSudokuSolution.txt ), and run the program using the following command:
java CheckSudokuSolution < CheckSudokuSolution.txt
input file
7.8 Multidimensional Arrays
A two-dimensional array consists of an array of one-dimensional arrays and a three-
dimensional array consists of an array of two-dimensional arrays.
Key
Point
In the preceding section, you used a two-dimensional array to represent a matrix or a table.
Occasionally, you will need to represent n -dimensional data structures. In Java, you can cre-
ate n -dimensional arrays for any integer n .
The way to declare two-dimensional array variables and create two-dimensional arrays can
be generalized to declare n -dimensional array variables and create n -dimensional arrays for
For example, you may use a three-dimensional array to store exam scores for a class
of six students with five exams, and each exam has two parts (multiple-choice and essay). The
following syntax declares a three-dimensional array variable scores , creates an array, and
assigns its reference to scores .
n
7=
3.
double [][][] scores = new double [ 6 ][ 5 ][ 2 ];
You can also use the short-hand notation to create and initialize the array as follows:
double [][][] scores = {
{{ 7.5 , 20.5 }, { 9.0 , 22.5 }, { 15 , 33.5 }, { 13 , 21.5 }, { 15 , 2.5 }},
{{ 4.5 , 21.5 }, { 9.0 , 22.5 }, { 15 , 34.5 }, { 12 , 20.5 }, { 14 , 9.5 }},
{{ 6.5 , 30.5 }, { 9.4 , 10.5 }, { 11 , 33.5 }, { 11 , 23.5 }, { 10 , 2.5 }},
{{ 6.5 , 23.5 }, { 9.4 , 32.5 }, { 13 , 34.5 }, { 11 , 20.5 }, { 16 , 7.5 }},
{{ 8.5 , 26.5 }, { 9.4 , 52.5 }, { 13 , 36.5 }, { 13 , 24.5 }, { 16 , 2.5 }},
{{ 9.5 , 20.5 }, { 9.4 , 42.5 }, { 13 , 31.5 }, { 12 , 20.5 }, { 16 , 6.5 }}};
scores[0][1][0] refers to the multiple-choice score for the first student's second exam,
which is 9.0 . scores[0][1][1] refers to the essay score for the first student's second
exam, which is 22.5 . This is depicted in the following figure:
Which student
Which exam
Multiple-choice or essay
scores [i] [j] [k]
A multidimensional array is actually an array in which each element is another array. A three-
dimensional array consists of an array of two-dimensional arrays. A two-dimensional array
consists of an array of one-dimensional arrays. For example, suppose x = new
int[2][2][5] , and x[0] and x[1] are two-dimensional arrays. X[0][0] , x[0][1] ,
x[1][0] , and x[1][1] are one-dimensional arrays and each contains five elements.
 
 
 
Search WWH ::




Custom Search