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





