Jagged Array in Java

How to Create and Use a Jagged Array in Java

Rate this post

Arrays are important in Java. They store data in a structured way. A normal two-dimensional array has equal columns. But sometimes, rows need different column sizes. This is where a jagged array is useful.

What is a Jagged Array?

A jagged array is an array of arrays. Each row can have a different length. Unlike a 2D array, it is not rectangular. In other words, each sub-array can hold a different number of elements.

Example:

  • A 2D array has rows of equal size.
  • A jagged array allows rows of unequal size.

This makes it flexible for many real-world problems.

Why Use a Jagged Array?

Jagged arrays are helpful in many cases:

  • They save memory because rows can be shorter.
  • They handle data that is not rectangular.
  • They give flexibility in storing irregular data.
  • They are easy to manage and access.

How to Declare a Jagged Array

The syntax is simple. You declare an array of arrays.

int[][] jaggedArray = new int[3][];

Here, we created an array with three rows. But we have not defined column sizes yet.

Initializing a Jagged Array

Each row is assigned separately. Rows can have different sizes.

jaggedArray[0] = new int[2]; // First row with 2 elements
jaggedArray[1] = new int[4]; // Second row with 4 elements
jaggedArray[2] = new int[3]; // Third row with 3 elements

Now, the jagged array is ready. Each row has a different size.

Assigning Values in a Jagged Array

We can assign values to each element using loops.

jaggedArray[0][0] = 10;
jaggedArray[0][1] = 20;
jaggedArray[1][0] = 30;
jaggedArray[1][1] = 40;
jaggedArray[1][2] = 50;
jaggedArray[1][3] = 60;
jaggedArray[2][0] = 70;
jaggedArray[2][1] = 80;
jaggedArray[2][2] = 90;

Traversing a Jagged Array

You can use nested loops to access values.

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

Output

10 20
30 40 50 60
70 80 90

Example: Creating a Jagged Array with Values

You can also initialize a jagged array directly.

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

This declares and initializes the array in one step. Each row has different elements.

Jagged Array with Different Data Types

Jagged arrays can store any type. Let us see a string example.

String[][] words = new String[2][];
words[0] = new String[]{“Hello”, “World”};
words[1] = new String[]{“Java”, “Programming”, “Language”};
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words[i].length; j++) {
System.out.print(words[i][j] + ” “);
}
System.out.println();
}

Output

Hello World
Java Programming Language

This shows that jagged arrays are flexible and work with different data types.

Real-Life Example of Jagged Array

Imagine storing marks of students. Each student has a different number of subjects. A jagged array is perfect.

int[][] marks = new int[3][];
marks[0] = new int[]{80, 90};
marks[1] = new int[]{75, 85, 95};
marks[2] = new int[]{60};
for (int i = 0; i < marks.length; i++) {
System.out.print(“Student “ + (i+1) + “: “);
for (int j = 0; j < marks[i].length; j++) {
System.out.print(marks[i][j] + ” “);
}
System.out.println();
}

Output

Student 1: 80 90
Student 2: 75 85 95
Student 3: 60

This is a common use case for a jagged array.

Jagged Array vs 2D Array

Feature 2D Array Jagged Array
Structure Rows of equal size Rows can have different sizes
Memory Fixed Flexible
Syntax int[3][3] int[3][]
Use Case Tables and grids Uneven data sets

Advantages of Jagged Arrays

  1. Memory Efficient: Use only the required memory.
  2. Flexible: Rows can vary in length.
  3. Easy to Use: Works like an array of arrays.
  4. Supports Any Data Type: Works with integers, strings, or objects.

Common Mistakes

  1. Forgetting to initialize each row separately.
  2. Accessing an element in a row that does not exist.
  3. Assuming rows are of equal length like a 2D array.

Best Practices

  • Always check the row length before accessing.
  • Initialize rows clearly to avoid confusion.
  • Use meaningful examples to represent data.

Conclusion

A Jagged Array in Java is an array of arrays with different lengths. It is flexible and memory-friendly. It is useful when data is not rectangular. Jagged arrays work with any type, such as numbers or strings. They are helpful in real-world scenarios like storing student marks. Understanding and using jagged arrays will make your Java programs more efficient and powerful.

Back To Top