6.10. Two-Dimensional Arrays

 
[Page 199 ( continued )]

6.11. (Optional) Multidimensional Arrays

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 create 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 n > = 3. For example, the following syntax declares a three-dimensional array variable scores , creates an array, and assigns its reference to scores :

   double   [][][] scores =   new double   [   10   ][   5   ][   2   ]; 

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, each of which is an array of one-dimensional arrays. For example, suppose x = new int[2][2][6] , 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. x.length is 2 , x[0].length and x[1].length are 2 , and X[0][0].length , x[0][1].length , x[1][0].length , and x[1][1].length are 6 .


[Page 200]

6.11.1. Example: Computing Student Scores

Listing 6.12 gives a program that calculates the total score for the students in a class. Suppose the scores are stored in a three-dimensional array named scores . The first index in scores refers to a student, the second refers to an exam, and the third refers to a part of the exam. Suppose there are seven students, five exams, and each exam has two parts : a multiple-choice part and a programming part. scores[i][j][0] represents the score on the multiple-choice part for the i 's student on the j 's exam. scores[i][j][1] represents the score on the programming part for the i 's student on the j 's exam. The program processes the scores array for all the students. For each student, it adds the two scores from all exams to totalScore and displays totalScore . Your program displays the total score for each student, as shown in Figure 6.18.

Listing 6.12. TotalScore.java

Figure 6.18. The program displays the total score for each student.


To understand this example, it is essential to know how data in the three-dimensional array are interpreted. scores[0] is a two-dimensional array that stores all the exam scores for the first student. scores[0][0] is {7.5, 20.5} , a one-dimensional array, which stores two scores for the two parts of the first student's first exam. scores[0][0][0] is 7.5, which is the score for the first part of the first student's first exam. scores[5] is a two-dimensional array that stores all the exam scores for the sixth student. scores[5][4] is {16, 6.5} , a one-dimensional array, which stores two scores for the two parts of the sixth student's fifth exam. scores[5][4][1] is 6.5, which is the score for the second part of the sixth student's fifth exam.


[Page 201]

The statement in lines 4 “11 declares, creates, and initializes a three-dimensional array of double values and assigns the reference to scores of the double[][][] type.

The scores for each student are added in lines 16 “18, and the result is displayed in lines 20 “21. The for loop in line 14 process the scores for all the students.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net