Bird
Raised Fist0
Javaprogramming~10 mins

Two-dimensional arrays in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Two-dimensional arrays
Declare 2D array
↓
Allocate memory for rows
↓
Allocate memory for columns in each row
↓
Access or modify element at [row
↓
Use nested loops to traverse rows and columns
↓
Process or print elements
↓
End
This flow shows how a two-dimensional array is declared, allocated, accessed, and traversed using nested loops.
Execution Sample
Java
int[][] matrix = new int[2][3];
matrix[0][1] = 5;
for(int i=0; i<2; i++) {
  for(int j=0; j<3; j++) {
    System.out.print(matrix[i][j] + " ");
  }
  System.out.println();
}
This code creates a 2x3 integer array, sets one element, and prints all elements row by row.
Execution Table
StepActionIndices (i,j)Value Assigned/AccessedOutputNotes
1Declare 2D array---matrix created with 2 rows and 3 columns, all initialized to 0
2Assign value[0,1]5-matrix[0][1] set to 5
3Start outer loopi=0--Begin first row
4Start inner loopi=0, j=0-0 Print matrix[0][0], default 0
5Inner loopi=0, j=1-5 Print matrix[0][1], assigned 5
6Inner loopi=0, j=2-0 Print matrix[0][2], default 0
7End inner loopi=0-Line breakEnd of first row print
8Outer loopi=1--Begin second row
9Inner loopi=1, j=0-0 Print matrix[1][0], default 0
10Inner loopi=1, j=1-0 Print matrix[1][1], default 0
11Inner loopi=1, j=2-0 Print matrix[1][2], default 0
12End inner loopi=1-Line breakEnd of second row print
13End outer loop---All rows printed, loops end
💡 Outer loop i=2 is not less than 2, so loop ends
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 11Final
matrix[0][0]00000
matrix[0][1]05555
matrix[0][2]00000
matrix[1][0]00000
matrix[1][1]00000
matrix[1][2]00000
i-0012
j--22-
Key Moments - 3 Insights
Why do we need two loops to print a 2D array?
Because a 2D array has rows and columns, the outer loop goes through rows and the inner loop goes through columns. See execution_table steps 3-12 where nested loops access each element.
What happens if we try to access matrix[2][0]?
It causes an error because the array has only 2 rows (indices 0 and 1). The loops stop at i=2 as shown in exit_note.
Why is matrix[0][1] 5 but others are 0?
Because only matrix[0][1] was assigned 5 at step 2; all other elements keep default int value 0 as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what value is printed for matrix[0][1]?
A0
B1
C5
DError
💡 Hint
Check the 'Value Assigned/Accessed' and 'Output' columns at step 5 in execution_table.
At which step does the outer loop variable i become 1?
AStep 8
BStep 3
CStep 12
DStep 13
💡 Hint
Look at the 'Action' and 'Indices (i,j)' columns in execution_table for when i changes.
If we change the array size to new int[3][3], how many times will the inner loop run per outer loop iteration?
A2
B3
C6
D9
💡 Hint
Inner loop runs for the number of columns, check the code in execution_sample and relate to array dimensions.
Concept Snapshot
Two-dimensional arrays in Java are arrays of arrays.
Declare with: int[][] name = new int[rows][cols];
Access elements with name[row][col].
Use nested loops to traverse rows and columns.
Default int values are 0.
Indexing starts at 0 for both rows and columns.
Full Transcript
This lesson shows how to work with two-dimensional arrays in Java. First, you declare and allocate the array with rows and columns. Then you can assign values to specific positions using two indices. To read or print all elements, you use nested loops: the outer loop goes through each row, and the inner loop goes through each column in that row. The example code creates a 2x3 array, sets one element to 5, and prints all elements. The execution table traces each step, showing how loops run and values are accessed or printed. The variable tracker shows how each element changes or stays the same. Key moments clarify why nested loops are needed, what happens if you access out-of-bounds indices, and why only one element is changed. The quiz tests understanding of values printed, loop variables, and array size effects. Remember, two-dimensional arrays are like tables with rows and columns, and you always use two indices to work with them.

Practice

(1/5)
1. What does a two-dimensional array in Java represent?
easy
A. A collection of unrelated variables
B. A single list of elements
C. A table with rows and columns
D. A method to sort numbers

Solution

  1. Step 1: Understand the structure of two-dimensional arrays

    Two-dimensional arrays store data in rows and columns, like a grid or table.
  2. Step 2: Compare options with the definition

    Only A table with rows and columns correctly describes this as a table with rows and columns.
  3. Final Answer:

    A table with rows and columns -> Option C
  4. Quick Check:

    Two-dimensional array = table [OK]
Hint: Think of it as a spreadsheet with rows and columns [OK]
Common Mistakes:
  • Confusing with one-dimensional arrays
  • Thinking it's a list of unrelated variables
  • Mixing arrays with methods
2. Which of the following is the correct way to declare a two-dimensional array of integers in Java?
easy
A. int matrix[3][4];
B. int[][] matrix = new int[3][4];
C. int matrix = new int[3,4];
D. int[] matrix = new int[3][4];

Solution

  1. Step 1: Recall Java syntax for 2D array declaration

    Java uses int[][] name = new int[rows][columns]; to declare 2D arrays.
  2. Step 2: Check each option

    int[][] matrix = new int[3][4]; matches correct syntax. int matrix[3][4]; is C-style, invalid in Java. int matrix = new int[3,4]; uses comma, invalid. int[] matrix = new int[3][4]; declares a 1D array incorrectly.
  3. Final Answer:

    int[][] matrix = new int[3][4]; -> Option B
  4. Quick Check:

    Correct 2D array syntax = int[][] matrix = new int[3][4]; [OK]
Hint: Use double brackets [][] and new with sizes [OK]
Common Mistakes:
  • Using commas instead of separate brackets
  • Trying C-style declarations
  • Mixing 1D and 2D array syntax
3. What is the output of the following Java code?
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(arr[1][0]);
medium
A. 3
B. 5
C. 4
D. 2

Solution

  1. Step 1: Identify the element at arr[1][0]

    arr[1] is the second row: {3, 4}. The element at index 0 in this row is 3.
  2. Step 2: Confirm the printed value

    System.out.println prints arr[1][0], which is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    arr[1][0] = 3 [OK]
Hint: Remember indexing starts at 0 for rows and columns [OK]
Common Mistakes:
  • Confusing row and column indices
  • Starting count at 1 instead of 0
  • Mixing up elements in nested arrays
4. Find the error in the following code snippet:
int[][] grid = new int[2][3];
grid[2][1] = 5;
medium
A. Array index out of bounds error
B. Syntax error in declaration
C. Missing initialization of array
D. No error, code runs fine

Solution

  1. Step 1: Check array size and indices used

    Array grid has 2 rows (indices 0 and 1) and 3 columns (indices 0 to 2). Accessing grid[2][1] uses row index 2 which is out of bounds.
  2. Step 2: Identify the error type

    Accessing an invalid index causes ArrayIndexOutOfBoundsException at runtime.
  3. Final Answer:

    Array index out of bounds error -> Option A
  4. Quick Check:

    Index 2 invalid for 2-row array [OK]
Hint: Check array size before accessing indices [OK]
Common Mistakes:
  • Using indices equal to array length
  • Confusing rows and columns sizes
  • Assuming arrays auto-expand
5. Given a 2D array int[][] data = {{1,2},{3,4},{5,6}};, which code snippet correctly calculates the sum of all elements?
hard
A. int sum = 0; for(int i=0; i
B. int sum = 0; for(int i=0; i
C. int sum = 0; for(int i=0; i
D. int sum = 0; for(int i=0; i

Solution

  1. Step 1: Understand array dimensions and iteration

    data.length is number of rows (3). data[i].length is number of columns (2). Nested loops must iterate rows then columns.
  2. Step 2: Analyze each option's loop structure

    int sum = 0; for(int i=0; i
  3. Final Answer:

    The nested loops with outer loop on data.length and inner on data[i].length -> Option D
  4. Quick Check:

    Correct nested loops sum all elements [OK]
Hint: Outer loop rows, inner loop columns for 2D arrays [OK]
Common Mistakes:
  • Swapping row and column loops
  • Adding arrays directly to int
  • Using wrong indices in loops
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard