Skip to lesson

2D arrays: rows and columns

A 2D array stores values in rows and columns, like a classroom seat chart or a marks table. You reach one value with two indexes: marks[row][col].

Rows and columns

A class test has 3 students and 4 subjects: that is a table, not a list. A 2D array, int[][], stores it in rows and columns, with each row written in its own braces.

A 2D array marks with 3 rows (students) and 4 columns (subjects). marks[1][2] is 88: row 1, column 2, both counted from 0. marks.length is 3, the rows; marks[0].length is 4, the columns.

Example 1 · A marks table

MarksTable.javaJava
public class MarksTable {    public static void main(String[] args) {        int[][] marks = {            {78, 85, 62, 90},            {66, 71, 88, 59},            {55, 80, 74, 93}        };        System.out.println("Student 2, subject 3: " + marks[1][2]);        marks[2][0] = 60;        System.out.println("Student 3, subject 1: " + marks[2][0]);    }}
Output
Student 2, subject 3: 88
Student 3, subject 1: 60

What this code does

  • Each inner { } is one row: one student's 4 marks.
  • marks[1][2] is row 1, column 2: the second student's third subject, 88. The row index always comes first, and both start at 0.
  • marks[2][0] = 60; changes one box, like any array element.

Size and default values

A hall has 5 rows of 8 seats, all free before booking opens. new int[5][8] makes 5 rows of 8 zeros; seats.length gives the rows, and seats[0].length the columns of row 0.

Example 2 · Rows, columns and default values

HallSize.javaJava
public class HallSize {    public static void main(String[] args) {        int[][] seats = new int[5][8];        seats[4][7] = 1;        System.out.println("Rows: " + seats.length);        System.out.println("Seats per row: " + seats[0].length);        System.out.println(seats[0][0] + " " + seats[4][7]);    }}
Output
Rows: 5
Seats per row: 8
0 1

What this code does

  • The first number in new int[5][8] is the rows, the second the columns: 5 × 8 = 40 boxes, all 0.
  • seats[4][7] = 1; books the very last seat: row 4 and column 7 are the last indexes.

A 2D array is really an array of rows, and each row is an ordinary array. That is why seats[0], one row, has its own length.

Nested loops visit every element

To print the whole marks table, every row needs every column. Use nested loops (opens in a new tab): the outer loop picks the row r, and the inner loop runs over that row's columns.

Row totals, one box at a time

Step 1 of 13

Rowcol 0col 1col 2col 3
row 078856290
row 166718859
row 255807493
r
0
c
0
total
78
Row 0 starts: total is reset to 0, then adds marks[0][0] = 78, so total = 78.

Example 3 · Print a marks table

PrintTable.javaJava
public class PrintTable {    public static void main(String[] args) {        int[][] marks = {{78, 85, 62, 90}, {66, 71, 88, 59}, {55, 80, 74, 93}};        for (int r = 0; r < marks.length; r++) {            for (int c = 0; c < marks[r].length; c++) {                System.out.print(marks[r][c] + " ");            }            System.out.println();        }    }}
Output
78 85 62 90
66 71 88 59
55 80 74 93

What this code does

  • For each row r, the inner loop prints all the columns c of that row on one line.
  • println() after the inner loop ends the line, so the next row starts on a new one.

Example 4 · Total marks of each student

RowTotals.javaJava
public class RowTotals {    public static void main(String[] args) {        int[][] marks = {{78, 85, 62, 90}, {66, 71, 88, 59}, {55, 80, 74, 93}};        for (int r = 0; r < marks.length; r++) {            int total = 0;   // a fresh total for each student            for (int c = 0; c < marks[r].length; c++) {                total += marks[r][c];            }            System.out.println("Student " + (r + 1) + ": " + total);        }    }}
Output
Student 1: 315
Student 2: 284
Student 3: 302

What this code does

  • total is made inside the outer loop, so it starts again at 0 for every row.
  • The inner loop adds one row's marks; the total is printed after it, once per student.

Write the inner condition as c < marks[r].length, the length of this row. Each row is its own array, and rows may even have different lengths, as in {{1}, {1, 1}, {1, 2, 1}}. Step through the row totals below, then press Play on the run after it to watch r and c change line by line.

Trace the code

Watch r and c move, like a debugger

public class WalkTheTable {
public static void main(String[] args) {
int[][] marks = {{78, 85, 62}, {66, 71, 88}};
for (int r = 0; r < marks.length; r++) {
for (int c = 0; c < marks[r].length; c++) {
System.out.println("marks[" + r + "][" + c + "] = " + marks[r][c]);
}
System.out.println("Row " + r + " done");
}
}
}

Variables

marks
{{78, 85, 62}, {66, 71, 88}}

Output

(nothing printed yet)

Line 3marks has 2 rows (one per student) and 3 columns (one per subject).

Step 1 of 20

Next step

Check your understanding

10 quick questions