Module 08: 2D Array Flashcards
2D Arrays
How can you store arrays in arrays?
The additional set of brackets note that arrays will be stored in this array
int[][] gradeBook = {exam1, exam2…};
int[][] gradeBook = new int[2][5];
gradeBook[0] = exam1;
gradeBook[1] = exam2;
- 2: number of arrays in the array
- 5: number of values in each array
2D Arrays
What are 2D Arrays?
Arrays that store arrays
int[][] gradeBook = {{90, 87, 30},
{49, 99, 22},
{90, 90, 23}};
When accessing elements in a 2D array, we are accessing the row and then column:
gradebook[row][column];
- Row - find array
- Column - find value in array
2D Arrays
What is row-major order?
The process of traversing a 2D array by accessing all elements in a row before moving on to the next row
2D Arrays
What is column-major order?
The process of traversing a 2D array by accessing all values at the first column in every row, before moving to the next column
2D Arrays
What is the proper syntax to initialize an empty 6x6 2D String array?
- String[] array = new String[6][6];
- int[][] array = new int[6][6];
- String[][] array = new String[6];
- String[][] array = new String[6][6];
- String[][] array = new String[6][6];
2D Arrays
int[][] grid = new int[5][3]
What is the value of grid[0].length?
0
5
3
15
3
2D Arrays
A 2D array is initialized:
int [][] array = {{1,2,3},{4,5,6},{7,8,9}};
What is the result of array[0][1] + array[2][0]?
10
15
9
5
9
8.2 Traversing 2D Arrays
What are the two methods to traverse a 2D array?
“Row-Major Order” = Traversing an array across each row
“Column-Major Order” = Traversing an array down each row
Choice depends on the task
8.2 Traversing 2D Arrays
How would you use a row-major order to “print all the test scores for exam1?”
for(int index = 0; index < gradeBook[0].length; index++)
{
System.out.println(gradeBook[0][index]);
}
8.2 Traversing 2D Arrays
How would you use a column-major traversal to “print all the test scores for student 2?”
for(int index = 0; index < gradeBook.length; index++)
{
System.out.println(gradeBook[index][1]);
}
8.2 Traversing 2D Arrays
How do you use a nested loop to traverse a 2D array?
for(int row = 0; row < gradeBook.length; row++)
{
for(int column = 0; column < gradeBook[row].length; column++)
{
System.out.print(gradeBook[row][column]);
}
}
8.2 Traversing 2D Arrays
How do you use column-major to traverse an array (using a nested loop)?
for(int row = 0; row < gradeBook.length; row++)
{
for(int column = 0; column < gradeBook[row].length; column++)
{
System.out.print(gradeBook[column][row]);
}
}
8.2 Traversing 2D Arrays
How do you use an enhanced for loops to traverse a 2D array?
for(int[] row: gradeBook)
{
for(int col: row)
{
System.out.printl(col);
}
}
- Outer loop needs to be an array data type
- Inner loop needs to be the data type of the array
8.2 Traversing 2D Arrays
How do you use Linear Search in 2D arrays?
When applying Linear Search on 2D Arrays, each row must be accessed, then Linear Search can be applied to each row:
public String search(int[][], array2D, int key)
{
for(int rowIndex = 0; rowIndex < array2D.length; rowIndex++)
{
for(int colIndex = 0; colIndex < array2D[rowIndex].length; colIndex++)
{
if(array2D[rowIndex][colIndex] == key)
{
return key + “ is located at: “ + rowIndex + “,” + colIndex;
}
}
}
return key + “is not in this 2D array”;
}
8.2 Traversing 2D Arrays
How do you use common 2D algorithms to determine the min/max or compute the sum, average, or mode?
int[][] scores = {{2,3,4,5,6},{7,6,5,4,3}}
//Initialize value here: for(int[] row: scores)
{
for(int i = 0; i < row.lenght; i++)
//Preform calculation here
}
//Report results
8.2 Traversing 2D Arrays
How do you use common 2D array algorithms for:
- Determine if at least one element has a particular property
- Determine if all elements have a particular property
- Determine the number of elements meeting specific criteria
int[][] scores = {{2,3,4,5,6},{7,6,5,4,3}}
//Initialize tracher variable
for(int[] row: scores)
{
for(int i = 0; i < row.length; i++)
{
//Conditionally check properties
}
}
//Report results
8.2 Traversing 2D Arrays
How do you use common 2D array algorithms for:
- Shift or rotate elements left or right
- Reverse the order of the elements
int[][] scores = {{2,3,4,5,6},{7,6,5,4,3}}
int[][] alteredScores;
for(int[] row : scores)
{
for(int i = 0; i < row.length; i++)
//add values from grade to alteredArray
}
//Report results
8.2 Traversing 2D Arrays
A 2D double array is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array.
The creator of this 2D array would like to look at the height of the city park along the longitude 100.0 - which traversal method would make the most sense in order to do so?
- Row-Major Order
- Enhanced for loop
- Column-Major Order
- This isn’t possible given the 2D array.
- Column-Major Order
8.2 Traversing 2D Arrays
A 2D double array terrainMap is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array.
Which of the following would be a correct way to write the traversal for this 2D array?
(1)
for(double[] row: terrainMap)
{
for(double[] num: row)
{
System.out.print(num + “ “);
}
System.out.println();
}
(2)
for(double[] row: terrainMap)
{
for(double num: row)
{
System.out.print(num + “ “);
}
System.out.println();
}
(2)
for(double[] row: terrainMap)
{
for(double num: row)
{
System.out.print(num + “ “);
}
System.out.println();
}
2D Array Quiz
Question: 1
What would be a correct way to instantiate this 2D array?
1 0 10 0
3 8 38 0
int [][] table = {
{1, 0, 10, 0},
{3, 8, 38, 0}
};
3. int[][] popcorn = new int[8][10];
2D Array Quiz
Question: 3
We want to create a 2D double array with 6 rows and 7 columns and assign it to connectFour. Which of these is correct?
- double[][] connectFour = new double[6][7];
- double[][] connectFour = new connectFour[6][7];
- double[][] connectFour = double[6][7];
- new double[6][7] connectFour;
- double[6][7] connectFour = new double[][];
1. double[][] connectFour = new double[6][7];
2D Array Quiz
Question: 4
Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something[1][2]?
- 6.8
- 6.1
- 2.3
- 1.3
- The array does not have an element at that location.
4. 1.3
2D Array Quiz
Question: 5
Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something[2][1]?
- 6.8
- 6.1
- 2.3
- 1.3
- The array does not have an element at that location.
2. 2.3
2D Array Quiz
Question: 6
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something.length?
5
4
3
12
8
3
2D Array Quiz
Question: 7
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something[2].length?
5
4
3
12
8
5
2D Array Quiz
Question: 8
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
How do you replace the value 13.21 with 8.8?
- something[2][3] = 8.8;
- something[2][4] = 8.8;
- something[13.21] = 8.8;
- something[3][4] = 8.8;
- something[3][3] = 8.8;
- something[2][3] = 8.8;
2D Array Quiz
Question: 9
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
We want to replace the row {6.1, 10.2, 1.3, -2.5, -9.9} with a complete new array that looks like {3.1, 4.1, 5.9, 2.6, 8.4}.
- double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};
something[2] = temp; - something[1] = {3.1, 4.1, 5.9, 2.6, 8.4};
- something[2] = new {3.1, 4.1, 5.9, 2.6, 8.4};
- double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};
something[1] = temp; - something[1][0] = 3.1;
something[1][1] = 4.1;
something[1][2] = 5.9;
something[1][3] = 2.6;
something[1][4] = 8.4;
4. double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};
something[1] = temp;
Not 5: This will produce the correct results but is not replacing the row with a new array, it just replaces the values.
2D Array Quiz
Question: 11
Consider the following code segment, which is intended to create and initialize the 2D array words where the length of each word corresponds to the product of the indices of the row and the column it resides in.
String[][] words = /*missing code */;
Which of the following initializer lists could replace /*missing code*/ so that the code segment works as intended?
- {{“”, “a”, “as”}, {“”, “b”, “be”}, {“”, “d”, “don”}}
- {{“a”, “as”, “ask”}, {“b”, “be”, “bet”}, {“d”, “do”, “don”}}
- {{“”, “”, “”}, {“”, “b”, “be”}, {“”, “do”, “dont”}}
- {{“a”, “a”, “a”}, {“”, “b”, “be”}, {“d”, “do”, “dont”}}
- {{“”, “”, “”}, {“”, “b”, “be”}, {“”, “d”, “do”}}
3. {{“”, “”, “”}, {“”, “b”, “be”}, {“”, “do”, “dont”}}
2D Array Quiz
Question: 12
Consider the following code segment, which is intended to display the word boards.
String[][] twoLetters = {{“ab”, “ac”, “ad”,”ar”,”af”},
{“bo”, “be”, “bi”, “ba”, “bu”},
{“ca”, “ce”, “ck”, “co”, “cs”},
{“da”, “ds”, “do”, “de”, “di”}};
System.out.println(/*Missing Code*/);
Which of the following can replace /*Missing Code*/ so the code segment works as intended?
- twoLetters[1][0] + twoLetters[0][3]+ twoLetters[3][1]
- twoLetters[0] + twoLetters[3]+ twoLetters[1]
- twoLetters[1][1] + twoLetters[0][4]+ twoLetters[3][2]
- twoLetters[0][1] + twoLetters[3][0]+ twoLetters[1][3]
- twoLetters[1][0] + twoLetters[0][3]+ twoLetters[twoLetters.length][1]
1. twoLetters[1][0] + twoLetters[0][3]+ twoLetters[3][1]
2D Array Quiz
Question: 13

Consider the following code segment:
How many instances of the number 5 will be stored in this 2D array?
4
5
3
1
0
4
2D Array Quiz
Question: 14
Consider the following code segment:
What will the result of the print statement be when this code segment is executed?
1
3
4
2
This will result in an IndexOutofBoundsException.

4
2D Array Quiz
Question: 15
How many times will the boolean expression printed from this code segment be true?

3
2D Array Quiz
Question: 16
Consider the following method, sumTo10, that traverses a 2D array checking to see if the sum of all integers in each array are equal to 10. The method will return true if all arrays within the 2D array sum to 10, and false otherwise.
For example, the 2D array {{2,8}, {4, 6}} would return true because the content of each array sums to 10. The method, however, doesn’t always work as intended. Which of the following 2D array input values does sumTo10 not work for?
- {{5, 5}, {3,7}}
- {{5, 2, 3}, {3, 1, 6}, {1, 1, 8}}
- {{-100, 0, 110}, {3, 2, 5}, {2, 1, 7}}
- {{9, 1, 0}, {4, 1, 5}}
- {{10}}

- {{9, 1, 0}, {4, 1, 5}}
2D Array Quiz
Question: 17
Which of the following traversals would print all of the elements in the 2D array of integers nums in column-major order?
for(int row = 0; row < nums[0].length; row++)
{
for(int col = 0; col < nums.length; col++)
{
System.out.println(nums[col][row]);
}
}
2D Array Quiz
Question: 19
What will be displayed as a result of executing this code segment?

101
2D Array Quiz
Question: 20

What will the value of each element in mystery be after the execution of the code segment?
{{1, 0, 0}
{2, 2, 2}
{1, 0, 3}}