Unit 8: 2D Array Flashcards
What will int[][] grid = new int[3][4] create?
It will make a 3x4 grid of ints
How to get a row from your 2D int array?
int[][] grid = {
{1,2,3,4},
{5,6,7,8}
};
How do you get an element within your 2D array?
int elem = grid[1][2];
Consider the following code segment, which is intended to create and initialize the two-dimensional (2D) integer array num so that columns with an even index will contain only even integers and columns with an odd index will contain only odd integers. int[][] num = / missing code /; Which of the following initializer lists could replace / missing code / so that the code segment will work as intended? A. {{0, 1, 2}, {4, 5, 6}, {8, 3, 6}} B. {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}} C. {{1, 3, 5}, {2, 4, 6}, {3, 5, 7}} D. {{2, 1, 4}, {5, 2, 3}, {2, 7, 6}} E. {{2, 4, 6}, {1, 3, 5}, {6, 4, 2}}
A. {{0, 1, 2}, {4, 5, 6}, {8, 3, 6}}
Consider the following code segment, which is intended to display “cat”.
String[][] keyboard = {{“q”, “w”, “e”, “r”, “t”},
{“a”, “s”, “d”, “f”, “g”},
{“z”, “x”, “c”, “v”, “b”}};
System.out.println(/ missing expression /);
Which of the following can replace / missing expression / so that the code segment works as intended?
A) keyboard[12] + keyboard[5] + keyboard[4]
B) keyboard[13] + keyboard[6] + keyboard[5]
C) keyboard[2][2] + keyboard[1][0] + keyboard[0][4]
D) keyboard[2][2] + keyboard[0][1] + keyboard[4][0]
E) keyboard[3][3] + keyboard[2][1] + keyboard[1][5]
C) keyboard[2][2] + keyboard[1][0] + keyboard[0][4]
How do you declare a 2D array?
datatype[][] variableName = new datatype[numberRows][numberCols];
How can you traverse through arrays though a tested for loop?
A for loop inside of another for loop. These are used to loop through all
the elements in a 2d array. One loop can work through the rows and the other the
columns.
How can you get an out of bounds error when traversing through an array?
This happens when a loop goes beyond the last valid index in an array. Remember that the last valid row index is arr.length - 1. The last valid column
index is arr[0].length - 1.