Jagged Array in Java

Last Updated : 6 Jan 2026

In Java, a jagged array is a type of multidimensional array in which each row can have a different number of columns. In this chapter, we will learn what a jagged array is, how it is declared and initialized, and how to work with jagged arrays using simple examples.

What is Jagged Array in Java?

A jagged array is a collection of arrays where each array may contain a varying number of elements. A two-dimensional array, in contrast, requires all rows and columns to have the same length.

Jagged arrays are also known as "ragged arrays" or "irregular arrays". They can be created by specifying the size of each array in the declaration.

For example, a jagged array with three rows can have the first row with three elements, the second with two elements, and the third with four elements.

Jagged Array Declaration

A jagged array is declared by first specifying the number of rows and then allocating each row separately with different column sizes.

Syntax

Here is the syntax of declaring a jagged array:

Here, the datatype is the data type of the elements in the array, numRows is the number of rows in the jagged array, and numColumns1, numColumns2, ..., numColumnsN are the number of columns in each row. Users should be aware that the number of columns in each row is flexible.

The first line creates an array of arrays with numRows rows. Each row is initialized to null by default.

The subsequent lines initialize each row of the jagged array. You create a new one-dimensional array of numColumns elements for each row and assign it to the corresponding row in the jagged array.

Ways to Initialize a Jagged Array

Apart from the standard approach of declaring and initializing a jagged array, there are several different ways to initialize a jagged array in Java.

1. Using Array Literals

We can use array literals to initialize the jagged array elements like this directly:

Here, we create a jagged array with three rows, where the first row has two elements, the second row has three elements, and the third row has four elements.

Example

Compile and Run

Output:

1 2 3
4 5
6 7 8 9

2. Using Nested Loops

We can also use nested loops to initialize the jagged array elements when the elements are to be programmatically generated. Utilizing such an approach can be beneficial.

Here, we design a jagged array with three rows, the first of which contains one element, the second two, and the third three. Using a simple formula, the nested loops generate the elements in the jagged array.

Example

Compile and Run

Output:

1
2 3
4 5 6
7 8 9 10

Jagged Array Using Java 8 Streams

With Java 8 streams, you can create and initialize a jagged array in a single line using the Arrays.stream and toArray methods like this:

Here, we make a three-row jagged array with two elements in the first row, three in the second row, and four in the third row. The Arrays.stream method converts each one-dimensional array into a stream of integers, and then the toArray method collects the stream into a jagged array.

Example

The following example demonstrates how to create jagged array using Java 8 streams.

Compile and Run

Output:

1 2 3
4 5
6 7 8 9

Dynamic Jagged Array

In Java, we can create a dynamic jagged array by creating an array of arrays, where each sub-array represents a row in the two-dimensional array. After thar, we can allocate memory to each sub-array to specify its number of columns.

Example

The following example shows the working of dynamic jagged array in Java.

Compile and Run

Output:

Enter the number of sub-arrays: 3
Enter the size of sub-array 1: 2
Enter the element at index 0 of sub-array 1: 1
Enter the element at index 1 of sub-array 1: 2
Enter the size of sub-array 2: 3
Enter the element at index 0 of sub-array 2: 3
Enter the element at index 1 of sub-array 2: 4
Enter the element at index 2 of sub-array 2: 5
Enter the size of sub-array 3: 1
Enter the element at index 0 of sub-array 3: 6
The jagged array is:
1 2
3 4 5
6

Jagged Arrays Vs Multi-Dimensional Arrays

A jagged array is not the same as a multi-dimensional array. There are some differences between Jagged array and multi-dimensional array that are described in the following table.

AspectJagged ArrayMulti-dimensional Array
StructureEach row (or sub-array) can have a different length.All rows must have the same number of columns.
Memory AllocationMemory is allocated separately for each sub-array, making it more memory-efficient when varying row sizes are needed.It allocates a continuous block of memory for all elements, potentially wasting memory when row lengths vary.
Declarationint[][] jaggedArray = {
    {1, 2, 3},
    {4, 5},
    {6, 7, 8, 9}
};
int[][] multiArray = new int[3][3]; // Fixed size: 3 rows, 3 columns
Accessing ElementsJagged Array: jaggedArray[row][column]Multi-dimensional Array: multiArray[row][column]
Uses CasesIt is useful when dealing with irregular data structures like variable-length rows in tables.It is ideal for grids, matrices, or fixed-size tables.