Java Reference
In-Depth Information
46 }
47
48 System.out.println( "Your birthday is " + day);
49 }
50 }
A three-dimensional array dates is created in Lines 8-28. This array stores five sets of
numbers. Each set is a 4-by-4 two-dimensional array.
The loop starting from line 33 displays the numbers in each set and prompts the user to
answer whether the birthday is in the set (lines 41-42). If the day is in the set, the first number
( dates[i][0][0] ) in the set is added to variable day (line 45).
8.8
Declare an array variable for a three-dimensional array, create a 4
*
6
*
5 int
Check
array, and assign its reference to the variable.
Point
8.9
Assume int[][][] x = new char[12][5][2] , how many elements are in the
array? What are x.length , x[2].length , and x[0][0].length ?
8.10
Show the output of the following code:
int [][][] array = {{{ 1 , 2 }, { 3 , 4 }}, {{ 5 , 6 },{ 7 , 8 }}};
System.out.println(array[ 0 ][ 0 ][ 0 ]);
System.out.println(array[ 1 ][ 1 ][ 1 ]);
C HAPTER S UMMARY
1.
A two-dimensional array can be used to store a table.
2.
A variable for two-dimensional arrays can be declared using the syntax:
elementType[][] arrayVar .
3.
A two-dimensional array can be created using the syntax: new elementType
[ROW_SIZE][COLUMN_SIZE] .
4.
Each element in a two-dimensional array is represented using the syntax:
arrayVar[rowIndex][columnIndex] .
5.
You can create and initialize a two-dimensional array using an array initializer with the
syntax: elementType[][] arrayVar = {{row values}, . . . , {row values}} .
6.
You can use arrays of arrays to form multidimensional arrays. For example, a variable
for three-dimensional arrays can be declared as elementType[][][] arrayVar , and
a three-dimensional array can be created using new elementType[size1][size2]
[size3] .
Q UIZ
Answer the quiz for this chapter online at www.cs.armstrong.edu/liang/intro10e/quiz.html .
P ROGRAMMING E XERCISES
*8.1
( Sum elements column by column ) Write a method that returns the sum of all the
elements in a specified column in a matrix using the following header:
public static double sumColumn( double [][] m, int columnIndex)
 
 
Search WWH ::




Custom Search