Java Tutorial
Java 2D Arrays
A 2D array is an array of arrays — rows of columns. Index with grid[row][col] and nest loops to visit every cell.
Declare and initialize
Write int[][] grid for a table of ints. You can size it with new int[rows][cols] or fill it with a nested initializer. First index is the row; second is the column.
Example
public class Main {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(grid[0][1]); // 2
System.out.println(grid[1][2]); // 6
}
}Walk every cell
Use nested loops: outer over rows, inner over columns. grid.length is the number of rows;grid[r].length is the length of that row.
Example
public class Main {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
System.out.print(grid[r][c] + " ");
}
System.out.println();
}
}
}for-each over rows
You can also for-each each row, then for-each the values in that row — handy when you do not need indices.
Example
public class Main {
public static void main(String[] args) {
int[][] grid = {{8, 1}, {3, 7}};
int sum = 0;
for (int[] row : grid) {
for (int n : row) sum += n;
}
System.out.println(sum); // 19
}
}Rows can have different lengths (a “jagged” array). Always use grid[r].length for that row instead of assuming every row matches grid[0].length.
Update a cell
Assignment uses the same indexing. Bounds still matter: going past a row or column length throwsArrayIndexOutOfBoundsException.
Example
public class Main {
public static void main(String[] args) {
int[][] scores = new int[2][3];
scores[0][0] = 90;
scores[1][2] = 85;
System.out.println(scores[0][0] + " " + scores[1][2]);
}
}grid[r][c] — row first, then column. Mixing them up is a silent logic bug until a value looks wrong.
Try It Yourself
Exercise: Create a 3×3 int[][] filled with 1 on the diagonal and 0 elsewhere, then print it.
Show solution
int[][] id = new int[3][3];
for (int i = 0; i < 3; i++) id[i][i] = 1;
for (int[] row : id) {
for (int n : row) System.out.print(n + " ");
System.out.println();
}Setting id[i][i] writes the main diagonal; other cells stay the default 0.
Key Takeaways
- A 2D array is an array of rows; index with
grid[row][col]. grid.lengthis rows;grid[r].lengthis that row’s length.- Nested loops (or nested for-each) visit every cell.
- Rows may differ in length — do not assume a perfect rectangle.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Statistics
Sum a data matrix
A 2D array is rows of columns. Walk every cell to get a grand total of counts.
Example
public class Main {
public static void main(String[] args) {
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
int sum = 0;
for (int[] row : grid) for (int n : row) sum += n;
System.out.println(sum);
}
}