Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In Java, a jagged array is a type of multidimensional array where each row can contain a different number of elements. It’s also referred to as “ragged array,” or “array of arrays” because it consists of arrays as its elements, each potentially having a different size.

In this tutorial, we’ll learn how to define, initialize, and work with jagged arrays in Java.

2. Memory Representation

As we know, an array in Java is nothing but an object, the elements of which could be either primitives or references. So, a 2D jagged array in Java can be thought of as an array of one-dimensional arrays.

Let’s see what a 2D jagged array looks like in memory:

memory representation of jagged array

Clearly, multiDimensionalArr[0] holds a reference to a single-dimensional array of size 2, multiDimensionalArr[1] holds a reference to another one-dimensional array of size 3, and so on.

This way, Java makes it possible for us to define and use jagged multi-dimensional arrays.

3. Declaring and Initializing Jagged Array

Below are different ways we can declare and initialize a jagged array.

3.1. Declaring and Initializing in a Single Step

Like other multi-dimensional arrays, we can declare and initialize a jagged array in a single step:

int[][] multiDimensionalArray = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};

Here, we’ve declared and initialized an array of three rows, each having a different number of elements.

3.2. Declaring Without Elements

Also, we can declare a jagged array without initializing it with elements:

int[][] multiDimensionalArray = new int[3][];

Here we declared a jagged array of three rows. In this case, the elements aren’t yet initialized, and by default, each element is set to null.

3.3. Initializing the Elements After Declaration

Next, let’s go further by both declaring and initializing the respective elements within multiDimensionalArr:

multiDimensionalArr[0] = new int[] {1, 2};
multiDimensionalArr[1] = new int[] {3, 4, 5};
multiDimensionalArr[2] = new int[] {6, 7, 8, 9};

Here, we assign specific arrays to each row.

3.4. Declaring and Initializing With Specific Sizes

We can also specify the size of each row without initializing the elements:

multiDimensionalArr[0] = new int[2];
multiDimensionalArr[1] = new int[3];
multiDimensionalArr[2] = new int[4];

Later, we can populate the arrays with values:

multiDimensionalArray[0][0] = 1;
multiDimensionalArray[0][1] = 2;

multiDimensionalArray[1][0] = 3;
multiDimensionalArray[1][1] = 4;
multiDimensionalArray[1][2] = 5;

multiDimensionalArray[2][0] = 6;
multiDimensionalArray[2][1] = 7;
multiDimensionalArray[2][2] = 8;
multiDimensionalArray[2][3] = 9;

3.5. Alternate Initialization Methods

A 2-D jagged array can also be initialized in one line:

int[][] multiDimensionalArray = new int[][] { 
  new int[] { 1, 2 }, 
  new int[] { 3, 4, 5 }, 
  new int[] { 6, 7, 8, 9 } 
};

Or more simply:

int[][] multiDimensionalArray = { 
  new int[] { 1, 2 }, 
  new int[] { 3, 4, 5 }, 
  new int[] { 6, 7, 8, 9 } 
};

Unlike the first, the second initialization omits the new int[][] keywords.

4. Printing Elements of Jagged Array

Moving on, we can print the elements of a jagged array to the console by using a for loop to iterate over each row and its elements:

for (int i = 0; i < multiDimensionalArray.length; i++) {
    for (int j = 0; j < multiDimensionalArray[i].length; j++) {
        System.out.print(multiDimensionalArray[i][j] + " ");
    }
    System.out.println();
}

In the code above, we loop through each row of the array and print its elements. Here’s the output:

1 2 
3 4 5 
6 7 8 9 

Alternatively, we can print each row using the Arrays.toString() method:

for (int index = 0; index < multiDimensionalArray.length; index++) {
    System.out.println(Arrays.toString(multiDimensionalArray[index]));
}

This approach avoids using the inner loop, and it prints each row as a formatted string:

[1, 2] [3, 4, 5] [6, 7, 8, 9]

5. Length of Elements

We can find the length of the arrays in a multi-dimensional array by iterating over the main array:

int[] findLengthOfElements(int[][] multiDimensionalArray) {
    int[] arrayOfLengths = new int[multiDimensionalArray.length];
    for (int i = 0; i < multiDimensionalArray.length; i++) {
        arrayOfLengths[i] = multiDimensionalArray[i].length;
    }
    return arrayOfLengths;
}

We can also find the length of arrays using streams:

Integer[] findLengthOfArrays(int[][] multiDimensionalArray) {
    return Arrays.stream(multiDimensionalArray)
      .map(array -> array.length)
      .toArray(Integer[]::new);
}

Here, we use map() to extract the length and collect them into an Integer[] array.

6. Copy a 2D Array

We can copy a 2D array using the Arrays.copyOf() method:

int[][] copy2DArray(int[][] arrayOfArrays) {
    int[][] copied2DArray = new int[arrayOfArrays.length][];
    for (int i = 0; i < arrayOfArrays.length; i++) {
        int[] array = arrayOfArrays[i];
        copied2DArray[i] = Arrays.copyOf(array, array.length);
    }
    return copied2DArray;
}

We can also achieve this by using streams:

Integer[][] copy2DArray(Integer[][] arrayOfArrays) {
    return Arrays.stream(arrayOfArrays)
      .map(array -> Arrays.copyOf(array, array.length))
      .toArray(Integer[][]::new);
}

The stream version performs the same deep copy operation in a functional style.

7. Conclusion

In this article, we looked at what jagged arrays are, how they look in memory, and how we can define, initialize, and use them.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW