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