Let's get started with a Microservice Architecture with Spring Cloud:
Jagged Arrays in Java
Last updated: July 30, 2025
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:
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.
















