100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Multidimensional Arrays in Java

Understand how Java implements multidimensional arrays as arrays of arrays, enabling jagged arrays and matrix-style data access.

Arrays & StringsBeginner9 min readJul 7, 2026
Analogies

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

java
// 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

java
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

text
1 2 3 
4 5 6 
Row length: 1
Row length: 2
Row length: 3

6. 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

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#MultidimensionalArraysInJava#Multidimensional#Arrays#Syntax#Explanation#DataStructures#StudyNotes#SkillVeris