Java Reference
In-Depth Information
11 {
17
,
19
,
21
,
23
},
12 {
25
,
27
,
29
,
31
}},
13 {{
2
,
3
,
6
,
7
},
14 {
10
,
11
,
14
,
15
},
15 {
18
,
19
,
22
,
23
},
16 {
26
,
27
,
30
,
31
}},
17 {{
4
,
5
,
6
,
7
},
18 {
12
,
13
,
14
,
15
},
19 {
20
,
21
,
22
,
23
},
20 {
28
,
29
,
30
,
31
}},
21 {{
8
,
9
,
10
,
11
},
22 {
12
,
13
,
14
,
15
},
23 {
24
,
25
,
26
,
27
},
24 {
28
,
29
,
30
,
31
}},
25 {{
16
,
17
,
18
,
19
},
26 {
20
,
21
,
22
,
23
},
27 {
24
,
25
,
26
,
27
},
28 {
28
,
29
,
30
,
31
}}};
29
30
// Create a Scanner
31 Scanner input =
new
Scanner(System.in);
32
33
for
(
int
i =
0
; i <
5
; i++) {
34 System.out.println(
"Is your birthday in Set"
+ (i +
1
) +
"?"
);
35
for
(
int
j =
0
; j <
4
; j++) {
36
for
(
int
k =
0
; k <
4
; k++)
37 System.out.printf(
"%4d"
,
Set i
dates[i][j][k]
);
38 System.out.println();
39 }
40
41 System.out.print(
"\nEnter 0 for No and 1 for Yes: "
);
42 answer = input.nextInt();
43
44
if
(answer ==
1
)
45 day +=
add to day
dates[i][
0
][
0
]
;
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).
7.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
7.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
?
7.10
Show the printout 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
]);