2D arrays: rows and columns
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
Example 1 · A marks table
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]); }}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
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]); }}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
| Row | col 0 | col 1 | col 2 | col 3 |
|---|---|---|---|---|
| row 0 | 78 | 85 | 62 | 90 |
| row 1 | 66 | 71 | 88 | 59 |
| row 2 | 55 | 80 | 74 | 93 |
- r
- 0
- c
- 0
- total
- 78
Example 3 · Print a marks table
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(); } }}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 columnscof 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
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); } }}Student 1: 315 Student 2: 284 Student 3: 302
What this code does
totalis 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
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).
Next step
Check your understanding
10 quick questions