Lessons / Computer Science / IB Computer Science | Year 2 (Senior) / 2D Arrays in Java · part 2/8
2D Arrays in Java: Nested Loops

Plays on through the playlist · IB Computer Science · 2D Arrays in Java (SL & HL) (2 videos) on YouTube
What you'll learn
IB Computer Science B2.2, 2D arrays in Java: nested loops (part 2 of 8, for SL and HL). The two lengths of a 2D array, grid.length and grid[0].length, and the nested loop that visits every element in row-major and column-major order. With a 6-mark exam-style question solved step by step.
In this lesson
Answers to the practice questions
- d.length is 6 [1]; d[0].length is 9 [1].
- Outer loop for (int c = 0; c < grid[0].length; c++) [1]; inner loop for (int r = 0; r < grid.length; r++) [1]; System.out.print(grid[r][c] + " "); with row first, then column [1].
- 28.
Try them at the end of the video first. Questions are original, in the style of the exam; marks are estimates.
Read the lesson (full transcript)
Two D arrays, nested loops. This answer scores zero.
The student counted the numbers in one row. Can you see the mistake?
Grid dot length counts the rows, not the numbers in a row. There are two rows. So the answer is two.
Here is the secret of two D arrays in Java. A two D array is a normal array, and each of its elements is another array. One array for each row.
So grid, zero, with one pair of brackets, is not a number. It is the whole first row.
That gives two lengths. Grid dot length is the number of rows. Grid zero dot length is the number of elements in row zero, so it is the number of columns.
Careful here. Students swap these two in every exam. Rows: grid dot length. Columns: grid zero dot length.
Small task. A grid is made with new int, five, two. What is grid dot length?
Five. Five rows.
To visit every element you need two loops, one inside the other. The outer loop chooses a row. The inner loop moves along that row, one column at a time.
Watch the counters. r is zero, and c goes zero, one, two. Three, seven, two. The inner loop ends. Now r is one, and c starts again from zero. Eight, one, five.
The outer loop is slow. The inner loop is fast. Like a clock: for each step of the slow hand, the fast hand goes all the way round. This order is called row-major order.
Careful here. Use less than, not less than or equal. The last row is the length minus one. With less than or equal, the loop goes one step too far, and the program crashes.
Small task. A grid has three rows and four columns. How many times does the print line run?
Twelve. Three times four.
Sometimes a question wants the columns first. Swap the two loops: the column loop outside, the row loop inside. Now the marker goes down each column. This is column-major order.
Careful here. The loops swap. The brackets never swap. Inside the brackets it is still row, then column.
Small task. In column-major order, which value is printed second?
Eight. Down the first column: three, then eight.
Now we trace. Three rounds.
I trace the two loops on a grid with three rows and two columns. One line of the table for each visit. r zero, c zero: two. r zero, c one: four. Then r is one: six, eight. Then r is two: one, three. Six elements, six lines. And look at c: zero, one, zero, one, zero, one. It starts again every time r moves.
Now together. The same grid, in column-major order. I do the first column: two, six, one. You finish the second column.
Four, eight, three. The output is two, six, one, four, eight, three.
Now alone. This code should print every element of the same grid, but it crashes. Find the mistake. Pause the video.
The two lengths are swapped. The row loop must stop at grid dot length, and the column loop at grid zero dot length. Test your code on a grid that is not square. A square grid hides this mistake.
A two mark question and three answers. Decide the marks.
Student A said nothing exact. Zero. Student B explained one of the two lengths. One mark. Student C explained both. Two marks.
When a question asks for a difference, write about both things.
Back to the question from the start. Pause the video and try all four parts.
Part a. Two inner pairs of curly brackets, so two rows. Grid dot length is two.
Part b. Each row has three elements. Grid zero dot length is three.
Part c. "Determine" means find the only possible answer. Row-major order, with a new line after each row. Three, seven, two: one mark. Eight, one, five on a new line: one mark.
Part d. Now column-major order, and no new lines. Three, eight, seven, one, two, five. One mark for going down the columns. One mark for the complete output.
Six out of six.
One screen. Grid dot length: rows. Grid zero dot length: columns. Two loops visit every element: the outer loop slow, the inner loop fast. To go by columns, swap the loops, never the brackets.
Three questions. Try them today, and again in three days. Next, part three: four patterns that use these two loops. Sum, count, maximum and search. See you there!