What is a Java 2D array?
A Java 2D array is an array whose elements are themselves arrays. The first index selects the row, and the second selects the column.
Study Java 2D Arrays: Traversal and Grid Algorithms with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a Java 2D array?
A Java 2D array is an array whose elements are themselves arrays. The first index selects the row, and the second selects the column.
How do you find 2D-array dimensions?
For scores, scores.length gives the number of rows, while scores[row].length gives the number of columns in that specific row.
What does new int[3][4] create?
new int[3][4] creates three rows with four integer elements per row. Each element initially contains 0.
What does scores[2][1] identify?
In a rectangular array, scores[2][1] refers to row 2, column 1. With zero-based indexing, this is the third row and second column.
What is row-major traversal?
Row-major traversal processes every element of row 0, then every element of row 1, continuing through the final row.
What order does column-major traversal use?
For grid = {{1, 2, 3}, {4, 5, 6}}, column-major order visits 1, 4, 2, 5, 3, 6.
Which bound should traverse a row safely?
The inner loop should use col < data[row].length. This uses the current row's column count instead of assuming rows and columns are equal.
How does rowSum process one row?
A row sum fixes the row index and varies the column index: for (int col = 0; col < data[row].length; col++) sum += data[row][col].
How does columnSum process one column?
A column sum fixes the column index and varies the row index: for (int row = 0; row < data.length; row++) sum += data[row][col].
Why does a 2D-array average need a double cast?
Cast the sum to double before division, as in (double) sum / count. Otherwise, integer division can discard the fractional part.
How should a maximum search be initialized?
Initialize max with data[0][0], not 0. This works even when every array value is negative.
What is short-circuiting in a 2D-array search?
A search for any matching element can return immediately when the condition is true. For example, containsNegative returns true at the first value below 0, then returns false if none is found.