INTERACTIVE JAVA LAB Loops & 2-D Arrays
Step 1 / 1

Java Source Code

System.out Console

2-D Array Memory Grid — Swipe or use Next ▶ to trace indices

Active cell grid[r][c] Processed Target match Out-of-bounds
Ready. Step through the code with Next ▶
2-D Arrays & Nested Loops — Core Rules
ExpressionMeaning in JavaCommon Pitfall
grid.lengthNumber of rows in the 2-D arrayConfusing grid.length with number of columns
grid[r].lengthNumber of columns in row rHardcoding column count fails on jagged arrays
grid[r][c]Row index r first, column index c secondSwapping [c][r] when traversing column-by-column
continue vs breakcontinue skips current iteration; break exits the innermost loop onlyExpecting break in inner loop to exit outer loop too
Classroom Teaching Guide
1. Array of arrays: Remind students that Java stores a 2-D array as a 1-D array of row references—which is why grid[r].length can vary per row.
2. Loop speed difference: Notice how the outer loop variable (r) stays fixed while the inner loop variable (c) cycles through all columns.
3. Controls: Use ← / → keys on desktop or swipe left/right on mobile phones.