Vocab: Vertical series of data in a 2D array
Column
True or False: You can not traverse a 2d array in column-major strictly using enhanced for loops
True
Initialize a new Int array with 5 rows and 4 columns
int[][] array = new int[4][5];
Vocab: An array nested within another Array
Inner Array
In order to traverse an arrays' column, what should your boolean be in your for loop?
column < array[0].length;
return the element in the 4th row and 3rd column
return array[3][2];
int[][] grid = {
{2, 3, 4, 4},
{4, 3, 4, 3},
{3, 2, 4, 3},
}
How many rows and columns are there in this array
3 rows and 4 columns
Write the outer and inner for loop to traverse a 2D String array, using enhanced for loops in row-major order
for (String[] row : array) {
for (String element : row) {
}
}
return the bottom right corner element
return array[array.length - 1][array[0].length - 1]
In context of rows and columns, array.length returns the total number of what in the array?
rows
int[][] array = {
{2, 3},
{4, 5}
};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = array[i][j] + i + j;
}
}
What will be printed when array is printed
{ {2, 4},
{5, 7} }
return the element in the 2nd row and 2nd to last column
return array[2][array[0].length - 2];
If you attempt to access an element which does not exist, what error does Java throw at you
ArrayIndexOutOfBoundsException
Print every element in a 2D double array using a regular for loop for the outer loop and an enhanced for loop for the inner loop.
Column-Major
for (int col = 0; col < array[0].length; col ++) {
for (double[] element : array) {
System.out.println(element[col]);
}
}
return the very center element assuming, there are an odd number of rows and columns.
return array[array.length / 2][array[0].length / 2];