I just announced the new Spring 5 modules in REST With Spring:


1. Overview

A jagged array in Java is a multi-dimensional array comprising arrays of varying sizes as its elements. It’s also referred to as “an array of arrays” or “ragged array”.

In this quick tutorial, we’ll look more in-depth into defining and working with jagged arrays.

2. Creating Jagged Array

Let’s start by looking at ways in which we can create a jagged array:

2.1. Using the Shorthand-Form

An easy way to define a jagged array would be:

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

Here, we’ve declared and initialized jaggedArr in a single step.

2.2. Declaration and then Initialization

We start by declaring a jagged array of size three:

int[][] jaggedArr = new int[3][];

Here, we’ve omitted to specify the second dimension since it will vary.

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

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

We can also simply declare its elements without initializing them:

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

These can then later be initialized, for example by using user inputs.

3. Memory Representation

How will the memory representation of our jaggedArr look like?

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

Our jaggedArr in memory would look similar to:

Clearly, jaggedArr[0] is holding a reference to a single-dimensional array of size 2, jaggedArr[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 arrays.

4. Iterating Elements

We can iterate a jagged array much like any other multi-dimensional array in Java.

Let’s try iterating and initializing the jaggedArr elements using user inputs:

void initializeElements(int[][] jaggedArr) {
    Scanner sc = new Scanner(System.in);
    for (int outer = 0; outer < jaggedArr.length; outer++) {
        for (int inner = 0; inner < jaggedArr[outer].length; inner++) {
            jaggedArr[outer][inner] = sc.nextInt();

Here, jaggedArr[outer].length is the length of an array at an index outer in jaggedArr.

It helps us to ensure that we are looking for elements only within a valid range of each sub-array, thereby avoiding an ArrayIndexOutOfBoundException.

5. Printing Elements

What if we want to print the elements of our jagged array?

One obvious way would be to use the iteration logic we’ve already covered. This involves iterating through each item within our jagged array, which itself is an array, and then iterating over that child array – one element at a time.

Another option we have is to use java.util.Arrays.toString() helper method:

void printElements(int[][] jaggedArr) {
    for (int index = 0; index < jaggedArr.length; index++) {

And we end up having a clean and simple code. The generated console output would look like:

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

6. Conclusion

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

As always, the source code of the examples presented can be found over on Github.

I just announced the new Spring 5 modules in REST With Spring:


Leave a Reply

Notify of