Jagged Array in Java
In Java, a Jagged array is an array that holds other arrays. When we work with a jagged array, one thing to keep in mind is that the inner array can be of different lengths. It is like a 2D array, but each row can have a different number of elements.
Example:
arr [][]= { {10,20}, {30,40,50,60},{70,80,90}};
Note: Here, the number of columns in each row is not the same, which means it is a Jagged array.
Diagrammatic Representation of Jagged Array in Memory
The image below demonstrates a Jagged array, where each row in a 2D array can have a different number of elements.

Declaration and Initialization of Jagged Array
The declaration of Jagged array is:
data_type array_name[][] = new data_type[n][]; // n= no. of rows
array_name[0] = new data_type[n1] //n1= no. of columns in row-1
array_name[1] = new data_type[n2] //n2= no. of columns in row-2
array_name[2] = new data_type[n3] //n3= no. of columns in row-3
.
.
.
and so on
The Alternative ways to initialize a Jagged array is listed below:
// Method 1
int arr_name[][] = new int[][] {
new int[] {10, 20, 30 ,40},
new int[] {50, 60, 70, 80, 90, 100},
new int[] {110, 120}
};
// Method 2
int[][] arr_name = {
new int[] {10, 20, 30 ,40},
new int[] {50, 60, 70, 80, 90, 100},
new int[] {110, 120}
};
// Method 3
int[][] arr_name = {
{10, 20, 30 ,40},
{50, 60, 70, 80, 90, 100},
{110, 120}
};
Now, let's discuss some examples of Jagged array for better understanding.
Examples of Jagged Array in Java
Following are Java programs to demonstrate the above concept.
Example 1: Simple Jagged Array
// Program to demonstrate 2-D jagged array in Java
class Geeks {
public static void main(String[] args){
// Declaring 2-D array with 2 rows
int arr[][] = new int[2][];
// Making the above array Jagged
arr[0] = new int[3];
arr[1] = new int[2];
// Initializing array
int count = 0;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;
// Printing the Array Elements
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
Output
0 1 2 3 4
Explanation: The above example shows how to create a jagged array, it takes an array with 2 rows, the first row has three elements and the second row has two elements. It fills the array with numbers starting from 0 and then prints them. This is the easiest way to handle uneven data in 2D array.
Example 2: Row i has i+1 columns
// Java program to demonstrate 2-D jagged
// array such that first row has 1 element, second
// row has two elements and so on
class Geeks {
public static void main(String[] args) {
int r = 5;
// Declaring 2-D array with 5 rows
int arr[][] = new int[r][];
// Creating a 2D array such that first row
// has 1 element, second row has two
// elements and so on
for (int i = 0; i < arr.length; i++)
arr[i] = new int[i + 1];
// Initializing array
int count = 0;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;
// Displaying the values of 2D Jagged array
System.out.println("Contents of 2D Jagged Array");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
Output
Contents of 2D Jagged Array 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Explanation: In the above example, we are creating a jagged array where each row will get one more element than the previous one, suppose the first row has one element then the second row has two element and then the third row has three element and so on. It fills the array with numbers starting from 0 and then prints them
Example 3: User Input Jagged Array
// Java Program to Implement User
// Defined Jagged Array
import java.util.Scanner;
// Main Class
public class Geeks {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of sub-arrays: ");
int numberOfArrays = scan.nextInt();
// Declare the jagged array
int[][] jaggedArray = new int[numberOfArrays][];
// Allocate memory to each sub-array
for (int i = 0; i < numberOfArrays; i++) {
System.out.print("Enter the size of sub-array " + (i + 1) + ": ");
int sizeOfSubArray = scan.nextInt();
jaggedArray[i] = new int[sizeOfSubArray];
}
// Initialize the elements of each sub-array
for (int i = 0; i < numberOfArrays; i++) {
System.out.println("Enter the elements of sub-array " + (i + 1) + ":");
for (int j = 0; j < jaggedArray[i].length; j++) {
jaggedArray[i][j] = scan.nextInt();
}
}
// Print the elements of the jagged array
System.out.println("The jagged array is:");
for (int i = 0; i < numberOfArrays; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println();
}
scan.close();
}
}
Input:
Enter the number of sub-arrays: 4
Enter the size of sub-array 1: 3
Enter the size of sub-array 2: 4
Enter the size of sub-array 3: 2
Enter the size of sub-array 4: 4Enter the elements of sub-array 1:
1 2 3Enter the elements of sub-array 2:
5 6 7 8Enter the elements of sub-array 3:
6 7Enter the elements of sub-array 4:
1 2 3 4
Output:
The jagged array is:
1 2 3
5 6 7 8
6 7
1 2 3 4
Advantages
The advantages of using a jagged array is listed below:
- With jagged array we can decide the size at runtime.
- Jagged array saves memory because we only create space for what we actually needed.
- Sometimes we have to work with data that has rows of different lengths, in that case we prefer using jagged array.
- Jagged arrays are faster because we only store what we needed and that why accessing the data becomes faster.
Note: Jagged arrays can make the code a bit harder to write and read, so it’s best to use them only when really needed.