java-arrays-multidimensionales-matrices

Multidimensional Arrays in Java: Matrices and Jagged Arrays

  • 4 min

A multidimensional array is an array whose elements are, in turn, other arrays.

In the previous article we learned how to manage data lists with one-dimensional arrays. Some structures need to represent rows, columns, or more dimensions.

Sometimes we need to represent an Excel table, a chessboard, the pixels of an image, or a coordinate map (x, y).

For these cases, Java allows us to go up a dimension. Welcome to Multidimensional Arrays.

What is a matrix really in Java?

Before seeing the code, we must understand the architecture.

If you come from C or C++, you might think that a 3x3 matrix is a contiguous memory block of 9 slots. In Java it is NOT.

In Java, strictly speaking, multidimensional arrays do not exist. What exists are Arrays of Arrays.

  • You have a “master” Array (the rows).
  • Each slot of that master array contains a reference to another, different array (the columns).

This detail allows you to create rows of different sizes, known as jagged arrays, as we will see later.

Matrices

These are the most common. They are defined with double brackets [][]. The first bracket represents the rows and the second the columns.

Declaration and Initialization

// Option A: Allocating space (Empty Matrix)
// We create a table with 3 rows and 4 columns
int[][] matrix = new int[3][4];

// Option B: With initial values (Visually it is a table)
int[][] board = {
    {1, 2, 3},  // Row 0
    {4, 5, 6},  // Row 1
    {7, 8, 9}   // Row 2
};
Copied!

Accessing Data

To access data, we use coordinates [row][column]. Remember we start at 0.

System.out.println(board[0][0]); // Prints 1 (Top-left corner)
System.out.println(board[1][2]); // Prints 6 (Row 1, Column 2)
Copied!

Traversing a Matrix

To visit all cells, we need nested loops. One loop handles going down the rows, and the other handles traversing the columns of that row.

int[][] matrix = {
    {10, 20, 30},
    {40, 50, 60}
};

// Outer loop: Traverses the ROWS
for (int i = 0; i < matrix.length; i++) {

    // Inner loop: Traverses the COLUMNS of row 'i'
    for (int j = 0; j < matrix[i].length; j++) {
        System.out.print(matrix[i][j] + " ");
    }
    System.out.println(); // Line break after each row
}
Copied!

Notice .length:

  • matrix.length: Gives us the number of rows (the size of the master array).
  • matrix[i].length: Gives us the number of columns for the current row.

Jagged Arrays

Because Java implements matrices as “Arrays of Arrays”, we are not forced to have all rows be the same size. We can create triangular or irregular structures. This is known as Jagged Arrays.

Imagine a cinema where row 1 has 2 seats, row 2 has 3 seats, and row 3 has 4 seats.

// Step 1: Define the rows, but leave the columns empty
int[][] cinema = new int[3][];

// Step 2: Initialize each row individually
cinema[0] = new int[2]; // Row 0: 2 seats
cinema[1] = new int[3]; // Row 1: 3 seats
cinema[2] = new int[4]; // Row 2: 4 seats

// Fill in some data
cinema[1][2] = 50; // Works
// cinema[0][2] = 50; // ERROR! Row 0 only has indices 0 and 1.
Copied!

This saves a lot of memory if you have sparse matrices (with many empty slots), since you only reserve space for what you use.

More Dimensions

We can keep adding brackets indefinitely (well, up to 255 dimensions per the JVM specification, but you will rarely go beyond 3).

A 3D array int[][][] is usually visualized as a cube or a set of pages.

// A 3x3x3 cube (e.g., Rubik's Cube)
int[][][] cube = new int[3][3][3];

cube[0][1][2] = 5; // Face 0, Row 1, Column 2
Copied!