Jagged Array in Java
Learn via video course
Abstract
Jagged Arrays are special types of Multidimensional arrays which have variable number of columns. It is an array of arrays where each element is an array and can be of a different size.
Scope of Article
- This article deals with the concepts of Jagged Arrays in Java.
- We will see what jagged arrays are, ways to initialize them, and examples of the same.
What is a Jagged Array?
A jagged Array is an array of arrays where each element is an array. It is a special type of Multidimensional array where there are a variable number of columns in each row.
They are also called as Ragged arrays.
Consider an example of a 2-D array with 2 rows, here each row can have a different number of columns, i.e, elements. They don't need to be equal in length.
It is important to understand that 2-D arrays have the same number of columns in each row. Whereas in jagged arrays, the rows have different numbers of columns.
Prerequisite
Before learning about Jagged arrays, we need to know what Arrays are.
- Arrays in Java are collections of similar types of elements. They are objects, and the elements are stored in contiguous memory locations.
- Indexing of Java starts from 0 and goes up to the length of the array minus 1 (0 to length-1).
- Arrays are stored in the heap memory.
- Types of arrays:
-
Single Dimensional Array
-
Multidimensional Array
-
Declaration and Initialisation of Jagged Array
The most common way to declare and initialize a jagged array is as follows -
Syntax
In order to declare a jagged array, we need to write its name preceded by its data type. The new keyword is used to create the object. Then we specify the number of rows and leave the column empty.
There are other ways to declare and initialize jagged arrays. Let us take a look at the other ways.
Example
Another way to declare and initialize a jagged array can be omitting the first new keyword.
Apart from the above methods, we can omit all the new keywords and initialize the value inside a jagged array.
We can also take input from the user using loops. We will discuss that in the upcoming section.
Pictorial Representation of Jagged Array in Memory
Consider the jagged array,
So, this array has 3 rows, and each row has a variable number of columns. They can be visualized in the following way.
The jagged array is stored in heap memory and each individual element of this jagged array is a one-dimensional array. Each 1-D array has a different size. This is what a jagged array is.
Read and Store Elements in a Dynamic Sized Jagged Array
Let us look at an example where we will create a 2-D jagged array. Here the zeroth row has 1 element, the first row has 2 elements, so on such that the nth row has n+1 elements.
We will do this by using a for loop. Hence a new array with the given size will be created.
Output
Jagged Array Example
In the previous section, we saw an example of a dynamic-sized jagged array. Now we will use another method to create a jagged array and print its elements.
Output
Printing the Elements of a Jagged Array
In order to print the elements of a Jagged array, we will use a nested 'for' loop. The inner 'for' loop will traverse over the elements in the column and the outer 'for' loop will traverse over the row.
The syntax for the same is -
Example
Consider a jagged array -
Output -
Explanation
In this jagged array, the outer 'for' loop will take control of the rows, and the inner 'for' loop will print the elements of the row one by one. Once the elements in the row are over, it will repeat the procedure for the rest of the rows.
Significance of Jagged Arrays
- Jagged arrays make storage easy as it provides a variable number of columns.
- The performance of a program can be improved as here we don't need to store unwanted elements and have flexibility over the number of elements in the row.
- Jagged arrays have a greater speed than multidimensional arrays and single-dimensional arrays as traversal is faster in jagged arrays.
Conclusion
- Jagged arrays are a special type of Multidimensional array where there are a variable number of columns in each row.
- They are also called Ragged arrays.
- We used jagged arrays in order to improve the performance.
FAQ's
1. What is the use of a Jagged array?
Ans- Jagged arrays are multidimensional arrays which can store multiple dimensions and can improve performance. They are used when we don't want each 1-D array to have the same length.
2. What is the Jagged array in Java?
Ans- Jagged arrays in Java are arrays of arrays. Jagged arrays are multidimensional arrays where each column has a variable size.
3. Why are Jagged arrays used?
Ans- Jagged arrays are helpful in enhancing the programs by letting them store several values in a specific manner.