True or false: you always need to use the "new" keyword when making a new 2d array
False
What are the 2 types of loops you can use to traverse through a 2d array?
For loops and while loops
What is the code to print the element at the first index of a 2d array named arr?
System.out.println(arr[0][0]);
What is always needed when swapping two variables in 2D Arrays? (specific addition within the code)
A temporary value
What is a 2d array?
An array of arrays.
What always goes before the name of the 2d array when making a new array?
Primitive type
What is the number to access the number of rows in a 2d array?
arr.length
In general, how do you loop through a 2d array to find a specific value? (think in terms of rows and columns)
Loop through every row, then each element in the row, and compare each element to the value you're looking for.
This nested control structure is commonly used to loop through each element of a 2D array.
Nested For loops.
How many elements are in a 2D array declared as new int[4][5]?
20 elements
What is the code to create a blank int 2d array with x number of rows and y number of columns?
int arr = new int[x][y];
What is the code to access the number of columns?
arr[0].length
Write the code to check all corners of a 5x5 2d array to see if at least are equal to unspecified integer x
if (arr[0][0] == x || arr[4][0] == 0 || arr[0][4] == 0 || arr[4][4] == 0){
System.out.print("...")
}
What happens if you access array[5][2] in a 4×4 array?
You get an ArrayIndexOutOfBoundsException.
What's a real-world use case for a 2D array in Java?
Grids for games, spreadsheet data, image pixels, seating charts, etc
how many total elements are there in arr if the code for instantiation is: int arr = new int[8][9]
72
What does the code look like to traverse through a 2d array using for loops
for (int i = 0; i < array2D.length; i++) {
// Inner loop iterates through columns for the current row
for (int j = 0; j < array2D[i].length; j++) {
System.out.print(array2D[i][j] + " ");
}
What is printed as a result of the following code segment?
for (int x : matrix[1]){
System.out.print(x + " ");
}
Each element in the second row of arr
How would you swap the first and last rows of a 2D array?
int[] temp = matrix[0];
matrix[0] = matrix[matrix.length - 1];
matrix[matrix.length - 1] = temp;
Write a method to check if a 2D array is rectangular.
public static boolean isRectangular(int[][] matrix) {
int length = matrix[0].length;
for (int[] row : matrix) {
if (row.length != length) return false;
}
return true;
}
what does the following array look like?
int arr = new int[3][3];
for (int row = 0; row < arr.length; row++)}
for (int col = 0; col < arr[0].length; col++){
arr[row][col] = 0
}
an array with 3 rows and 3 columns all with values 0.
What is the code for looping through a 2d array using an enhanced for loop?
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
Write the code to loop through a 2d array, searching for the number 6, starting from the bottom right and working your way up.
for (int row = arr.length - 1; row >= 0; row--) {
for (int col = arr[row].length - 1; col >= 0; col--) {
if (matrix[row][col] == 6) {
System.out.println("...")
}
}
}
What is the easiest way could you fill a 2D array with zeroes?
By default, new int[rows][cols] initializes all elements to 0.
How do you find the sum of the secondary diagonal elements of a square 2D array?
int sum = 0;
int n = matrix.length;
for (int i = 0; i < n; i++) {
sum += matrix[i][n - 1 - i];
}