Javatpoint Logo
Javatpoint Logo

Jagged Array in Java

A jagged array in Java is a collection of arrays where each array may contain a varied 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.

Declaration and Initialization of Jagged array

In Java, a jagged array can be declared and initialized using the following syntax:

Syntax:

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:

In addition to the standard approach of declaring and initializing a jagged array, as shown in the previous answer, several alternative ways exist to initialize a jagged array in Java.

1. Using array literals:

You 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.

  1. Create a new Java class named JaggedArrayExample.
  2. Define the main method inside the class.
  3. Declare a 2-D jagged of integers named jaggedArray with 3 rows.

Filename: JaggedArrayExample.java

Output:

1 2 3 
4 5 
6 7 8 9

2. Using nested loops:

You 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.

Filename: JaggedArrayDemo.java

Output:

1
2 3
4 5 6
7 8 9 10

3. 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.

  1. Create a new Java class named JaggedArrayExample.
  2. Define the main method inside the class.
  3. Declare a 2-D jagged of integers named jaggedArray with 3 rows.

Filename: JaggedArrayExample.java

Output:

1 2 3 
4 5 
6 7 8 9

4. Dynamic jagged array

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

Filename: DynamicJaggedArray.java

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

Advantages of Jagged Array

Jagged arrays have several advantages over multidimensional arrays with a fixed size:

  1. Memory Efficiency: Jagged arrays are more memory-efficient than multidimensional arrays because they only allocate memory for the elements they need. In a multidimensional array with a fixed size, all the memory is allocated, even if some elements are unused.
  2. Flexibility: Jagged arrays are more flexible than multidimensional arrays because they can have different sizes for each row. Jagged arrays enable the representation of non-rectangular or irregular data structures.
  3. Easy to Initialize: Jagged arrays are easy to initialize because you can specify the size of each row individually. The suitability of jagged arrays lies in their ability to store data that varies in size or is generated dynamically.
  4. Enhanced Performance: Jagged arrays can provide enhanced performance in certain scenarios, such as when you need to perform operations on each row independently or when you need to add or remove rows dynamically.
  5. More natural representation of data: In some cases, jagged arrays may be a more natural representation of data than rectangular arrays. For example, when working with irregularly shaped data such as geographic maps, a jagged array can be a more intuitive way to represent the data.
  6. Easier to manipulate: Jagged arrays can be easier to manipulate than rectangular arrays because they allow for more direct access to individual elements. With rectangular arrays, you may need to use more complex indexing or slicing operations to access subsets of the data.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA