A two-dimensional array stores values in a grid of rows and columns. You reach a value with two indices: grid[r][c], where r is the row and c is the column. Both count from 0, so grid[0][0] is the top-left element.
grid[r][c]
r
c
grid[0][0]
int[][] grid = {{1, 2, 3}, {4, 5, 6}}; System.out.println(grid[0][0]); // 1 (top-left) System.out.println(grid[1][2]); // 6 (row 1, col 2)
The first bracket picks the row; the second picks the column within that row.
▶ Try it: run this, then change a value in the grid or the indices you print, and re-run.
In grid[r][c], the first index r selects the...
For int[][] grid = {{1, 2, 3}, {4, 5, 6}};, what is grid[1][2]?
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
grid[1][2]
For {{1, 2, 3}, {4, 5, 6}}, match each access to its value.
{{1, 2, 3}, {4, 5, 6}}
Fill the indices to read the value 6 (row 1, column 2) from grid.
6
grid
True or false?
Type exactly what this program prints.
public class Main { public static void main(String[] a){ int[][] g = {{1, 2}, {3, 4}}; System.out.println(g[1][0]); System.out.println(g.length); }}