que-es-jagged-array-array-dentado

What is a Jagged Array?

  • 5 min

A Jagged Array, also known as a jagged or irregular matrix, is a multidimensional data structure in which each row can have a different number of elements.

When we learn to use multidimensional arrays, we almost always start by imagining a perfect grid: an Excel spreadsheet where all rows have the same number of columns. This is what we call a Rectangular Matrix.

But real life isn’t always so orderly. Sometimes we need to group data where each group has a different size. This is where the Jagged Array comes in.

programacion-jagged-array

Internally, a Jagged Array is literally an array of arrays. It is a collection where each element is, in turn, another array that can have its own independent length.

The Visual and Logical Difference

To understand it, let’s compare a Matrix (2D) with a Jagged Array:

  • Rectangular Matrix (int[,]): It is a solid block. If you declare a matrix, you have 9 cells. If row 1 only needs 2 pieces of data, you waste the third cell.
  • Jagged Array (int[][]): It is flexible. Row 0 can have 5 elements, row 1 can have 2, and row 2 can have 100.

Visually, if we align them to the left, the right edge appears “jagged,” hence its name.

  1. Memory Savings (if the data is irregular): If you have a triangular structure (e.g., row 1 has 1 data point, row 2 has 2…), using a rectangular matrix would force you to reserve space for the largest case, filling the rest with useless zeros. The Jagged Array only uses what’s necessary.
  2. Flexibility: You can swap entire rows simply by changing a reference, without copying all the data.
// Swapping row 0 and 1 is instantaneous
var temp = jagged[0];
jagged[0] = jagged[1];
jagged[1] = temp;
Copied!
  1. Performance (Cache Misses): Since the data is not together in memory, the processor cannot predict well which data will come next, causing more cache misses. For intensive linear algebra, it is slower than a rectangular matrix.
  2. Instantiation Complexity: You have to create the main array and then instantiate each of the subarrays (a loop of new).

How does it work in memory?

Although conceptually they may seem similar (or not) the difference in how they are implemented in memory is huge. The difference in memory management is enormous.

Data is usually stored in a single contiguous block of memory. The computer calculates the position with a mathematical formula:

It is very fast and efficient for the processor’s cache.

There is no single block; the rows can be scattered throughout RAM.

You have a Main array that contains references

Each reference points to another array located somewhere in memory

Each of these secondary Arrays holds the actual data

To read a piece of data, the processor has to make two trips: first find the reference to the row array, and then find the data within that row.

Jagged array syntax in different languages

Although the concept is universal, the syntax varies by language. Below, we will see examples in several popular languages.

In C#, jagged arrays are implemented as arrays of arrays.

using System;

class Program
{
    static void Main()
    {
        // Declare a jagged array with 3 rows
        int[][] jaggedArray = new int[3][];

        // Initialize each row with a subarray of different length
        jaggedArray[0] = new int[] { 1, 2, 3 };
        jaggedArray[1] = new int[] { 4, 5 };
        jaggedArray[2] = new int[] { 6 };

        // Access elements
        Console.WriteLine(jaggedArray[1][0]);  // Output: 4
    }
}
Copied!

In .NET, the Jagged Array ([][]) is often, paradoxically, faster than the Matrix ([,]) for simple operations due to specific CLR (Common Language Runtime) optimizations, unless you use unsafe pointers.

But in C++, a flat matrix always wins in raw speed.

In Java, jagged arrays are implemented as arrays of arrays.

public class Main {
    public static void main(String[] args) {
        // Declare a jagged array with 3 rows
        int[][] jaggedArray = new int[3][];

        // Initialize each row with a subarray of different length
        jaggedArray[0] = new int[] { 1, 2, 3 };
        jaggedArray[1] = new int[] { 4, 5 };
        jaggedArray[2] = new int[] { 6 };

        // Access elements
        System.out.println(jaggedArray[1][0]);  // Output: 4
    }
}
Copied!

In Python, jagged arrays are implemented as lists of lists.

# Declare a jagged array with 3 rows
jagged_array = [
    [1, 2, 3],
    [4, 5],
    [6]
]

# Access elements
print(jagged_array[1][0])  # Output: 4
Copied!

In JavaScript, jagged arrays are implemented as arrays of arrays.

// Declare a jagged array with 3 rows
const jaggedArray = [
    [1, 2, 3],
    [4, 5],
    [6]
];

// Access elements
console.log(jaggedArray[1][0]);  // Output: 4
Copied!

In JavaScript, technically real rectangular matrices do not exist at the basic data structure level. When you create an int[][] in Java, it is always a Jagged Array underneath, even if you initialize it as square.