By WebNest Studio Engineering Team
Java - Core Tutorial
Java Multidimensional Arrays
A multidimensional array in Java is really an array of arrays — there is no true rectangular block of memory the way there is in some other languages. A 2D array like int[][] grid is an array where each element is itself a reference to another int array, and understanding this "array of arrays" model explains both the flexibility and the quirks multidimensional arrays have in Java.
Because each inner array is an independent object, multidimensional arrays in Java do not have to be perfectly rectangular. Each row can have a different length, producing what is called a jagged array — a pattern that is unusual in some languages but completely normal and often useful in Java.
Why it matters
Grids, tables, game boards and matrices are naturally two-dimensional. Java does not have true 2D arrays; it has arrays whose elements are themselves arrays. Understanding that explains why rows can have different lengths, why a.length and a[0].length are different numbers, and why a row can be null.
Declaring and Creating Rectangular 2D Arrays
A rectangular grid is declared as int[][] grid = new int[3][4];, creating 3 rows of 4 columns each, all pre-filled with default values. Array literals also work for 2D data: int[][] grid = {{1, 2, 3}, {4, 5, 6}}; creates two rows directly. Access uses two index pairs, grid[row][col], where the first index selects the inner array and the second selects an element within it.
Jagged Arrays
Because each row is a separate array object, rows can have different lengths. You can create the outer array first with new int[3][] (note the empty second bracket), then assign each row its own array of any length. This is genuinely useful for data like a triangular table or a list of variable-length records, and it is a natural consequence of Java's "array of arrays" model rather than a special feature.
Iterating Multidimensional Arrays
A nested for or for-each loop is the standard way to visit every element: the outer loop walks the rows, and the inner loop walks the elements within the current row. Using for-each with int[] row : grid for the outer loop and int value : row for the inner loop reads cleanly and works correctly for jagged arrays too, since each row's own length is used automatically.
Example
Summing a rectangular 2D array, then building and printing a jagged array
public class MultiDimArrayDemo {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
int sum = 0;
for (int[] row : grid) {
for (int value : row) {
sum += value;
}
}
System.out.println("Sum: " + sum);
int[][] jagged = new int[3][];
jagged[0] = new int[]{1};
jagged[1] = new int[]{1, 2};
jagged[2] = new int[]{1, 2, 3};
for (int[] row : jagged) {
System.out.println(java.util.Arrays.toString(row));
}
}
}
Sum: 21
[1]
[1, 2]
[1, 2, 3]
Common Mistakes
- Assuming every row of a 2D array must have the same length — Java allows jagged arrays because each row is an independent array object.
- Trying to print a 2D array directly with Arrays.toString(), which only prints the outer array's references (like "[I@1b6d3586") instead of the actual numbers — Arrays.deepToString() is needed for nested arrays.
- Forgetting the empty brackets when creating a jagged array's outer shell, e.g. writing "new int[3]" instead of "new int[3][]" and then being unable to assign array rows.
- Mixing up row and column index order, e.g. writing grid[col][row] instead of grid[row][col], which silently accesses the wrong element instead of failing to compile.
Exercise
Add up the main diagonal of a square matrix — the elements where the row index equals the column index.
Expected output: 15 (1 + 5 + 9)
public class DiagonalSum {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
// TODO: add matrix[0][0], matrix[1][1], matrix[2][2] using a loop
System.out.println(sum);
}
}
Show hints
- On the main diagonal the row and column index are the same, so one loop variable is enough.
matrix.lengthgives the number of rows.
Show solution
public class DiagonalSum {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
sum += matrix[i][i];
}
System.out.println(sum); // 15
}
}
Quick quiz
1. After int[][] a = new int[3][4];, what is a.length?
- A. 7
- B. 4
- C. 12
- D. 3
Show answer
D. a.length is the size of the outer array, which is the number of rows: 3.
2. For the same array, what is a[0].length?
- A. 4
- B. 3
- C. 12
- D. 0
Show answer
A. a[0] is the first row, itself an array of 4 ints.
3. Is int[][] a = new int[3][]; valid?
- A. No, both sizes are required
- B. Yes, and each row is null until you create it
- C. Yes, and each row is an empty array
- D. Only for jagged arrays of Strings
Show answer
B. Only the first dimension is required. The three row references start as null, and you create each row separately, possibly with different lengths.
Interview questions
What is a jagged array?
Show answer
A two-dimensional array whose rows have different lengths. It is possible in Java because a 2D array is really an array of separate row arrays. You create the outer array first and then assign each row with whatever length you need, which saves space when the data is not rectangular.
How is a 2D array stored in memory in Java?
Show answer
As an array of references, where each reference points to another array object holding one row. The rows are separate objects on the heap and need not be next to each other. This differs from C, where a 2D array is one continuous block of memory.
Key Points to Remember
- Java implements multidimensional arrays as arrays of arrays, not as one contiguous rectangular block.
- Rows of a 2D array can have different lengths, producing a jagged array.
- Use Arrays.deepToString() to print nested arrays meaningfully; Arrays.toString() only shows one level.
- Nested loops (or nested for-each) are the standard way to visit every element of a multidimensional array.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering