0
0
Javaprogramming~15 mins

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

Choose your learning style8 modes available
flowchartConcept 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.
code_blocksExecution 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.
data_tableExecution 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
search_insightsVariable 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-
keyKey Moments - 3 Insights
Why do we need two loops to print a 2D array?
What happens if we try to access matrix[2][0]?
Why is matrix[0][1] 5 but others are 0?
psychologyVisual 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
photo_cameraConcept 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.
contractFull 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.