Uploaded by Ooi Leng Kai

APCSA Unit 8 2D Array

advertisement
2D Array | Worksheet
Name _____________________
1.
2
Date _________________
JAVA 2D array exercise.
Write the syntax to declare and initialize the following arrays.
a
An string array called myPet with the length 5.
b
A double array with values 2.3, 4.5, 6.8 and 9.0 called myScore
c
An integer array with the dimension of 3 x 3 called matrix
d
A 2D String array called worldStates with Washington and California on the first tier
and Beijing and Jiangsu on the second tier
e
A double array called tempStat with the size 3 x 6.
Based on the segment of code below answer the following question.
int[] grid = new int[5];
for (int i = 0; i < grid.length ; i++){
grid [i] = i*2;
System.out.print(grid[i])
}
a. What is the output? _________________________________________________
b. What is the value of grid.length?______________________________________
c. What is the value of grid[i] if the value of i is 3 ? ________________________
The line below is added to the code.
for (int temp : grid){
if (temp%3==0)
System.out.print(0);
System.out.print(temp);
}
d. What is the output for this extended code? ______________________________
e. Will it cause permanent changes to the value in grid? Why?
_________________________________________________________________________
f.
Write a segment of a code the change the value index 3 in grid to 0.
APCSA/UNIT-8/2DArray
2D Array | Worksheet
3
Based on the segment of code below answer the following question.
int[][] maxGrid = new int[5][3];
int count =0;
for (int i = 0; i < maxGrid.length ; i++){
for (int j = 0; j < maxGrid[i].length ; j++){
maxGrid[i][j] = count * 3;
count++;
}
}
a. What is the value assigned to each element in the array maxGrid?
b. From your answer, what is the value for the following array?
MaxGrid[0][0]
MaxGrid[1][2]
MaxGrid[2][0]
MaxGrid[4][2]
MaxGrid[4][3]
4
Based on the segment of code below answer the following question.
String[][] country = {{“France”, “Spain”, “Italy”},
{“China”, “Japan”, “South Korea”},
{“Thailand”, “Indonesia”, “Singapore”}};
a. What is the java code to get the country below:
Spain
Japan
Thailand
b. Write an enhance for loop to print out all the country.
c. Write a for loop to print out only France, Japan, and Singapore.
APCSA/UNIT-8/2DArray
Download