1. Introduction
A multidimensional array is an array whose elements are themselves arrays. The most common use case is a two-dimensional array, often used to represent a grid, matrix, or table of rows and columns.
Cricket analogy: A season's scorecard laid out as matches by innings, rows for each match and columns for each innings' runs, is like a 2D array representing a grid or table.
Unlike languages such as C that allocate a single contiguous block of memory for a 2D array, Java implements multidimensional arrays as true 'arrays of arrays'. Each row is an independent array object, which gives Java a unique and important capability: jagged arrays.
Cricket analogy: Unlike a stadium's fixed contiguous rows of seats, Java's 2D array is more like separate team buses, each bus (row) independently sized, so one squad's bus can carry more players than another's, enabling jagged arrays.
2. Syntax
// Rectangular 2D array: 3 rows, 4 columns
int[][] grid = new int[3][4];
// 2D array literal
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
// Jagged array: rows of different lengths
int[][] jagged = new int[3][];
jagged[0] = new int[]{1};
jagged[1] = new int[]{1, 2};
jagged[2] = new int[]{1, 2, 3};
// Accessing an element
int value = grid[1][2];3. Explanation
grid[i][j] means: take the array grid, get the i-th element (which is itself an int[] row array), then get the j-th element of that row array. Because each row is a separate array object, rows can have different lengths — this is called a jagged array.
Cricket analogy: grid[2][3] is like flipping to the 3rd match's bus (row 2) and pulling out the 4th player's stats (column 3); since each match's bus is a separate roster, one match can have more listed reserves than another, a jagged array.
grid.length gives the number of rows, and grid[i].length gives the number of columns in row i. For a rectangular array all rows have equal length, but for a jagged array grid[i].length can differ per row.
Cricket analogy: grid.length tells you how many matches are recorded, and grid[i].length tells you how many innings entries match i has; in a jagged season some rain-affected matches simply have fewer recorded innings than others.
Nested for-loops (or an outer loop over rows and an inner loop using row.length) are the standard way to traverse 2D arrays, which correctly handles jagged rows since each row's own length is used.
Exam trap: new int[3][4] pre-allocates all rows with 4 columns each (a true rectangular array). But new int[3][] only allocates the outer array of 3 row references, all initially null — you must assign each row separately before use, or you'll get a NullPointerException.
4. Example
public class MultiArrayDemo {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
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("Row length: " + row.length);
}
}
}5. Output
1 2 3
4 5 6
Row length: 1
Row length: 2
Row length: 36. Key Takeaways
- Java implements multidimensional arrays as arrays of arrays, not contiguous blocks.
- Each row of a 2D array is itself an independent array object.
- Jagged arrays allow rows with different lengths since rows are allocated separately.
- grid.length is the row count; grid[i].length is the column count of row i.
- new int[3][] allocates row references as null until you assign each row explicitly.
- Nested loops (using each row's own length) are the standard traversal pattern.
Practice what you learned
1. How does Java internally implement a 2D array like int[][] grid?
2. What does grid[i].length return for a 2D array grid?
3. Which feature is uniquely possible because Java implements multidimensional arrays as arrays of arrays?
4. After int[][] arr = new int[3][]; what is the state of arr[0]?
5. For int[][] m = new int[2][5];, what is m.length?
Was this page helpful?
You May Also Like
Arrays in Java
Learn how Java arrays store fixed-size, homogeneous collections of elements on the heap, including declaration, default values, and common runtime errors.
for Loop in Java
Understand Java's classic three-part for loop and the enhanced for-each loop for iterating over arrays and collections concisely.
Strings in Java
Understand Java's String class, its immutability, the string constant pool, and why == should never be used to compare string content.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics