This Tutorial Explains a Special Version of Multidimensional Array called Jagged Array. Learn to Create, Initialize & Use Jagged Arrays in your code:
We have already discussed Multi-dimensional arrays in Java in one of our previous tutorials. In this tutorial, we will discuss what is a Jagged Array and how to use it in your program.
This array is also known as “Ragged array” and is basically an array of arrays.
=> Take A Look At The Java Beginners Guide Here.
Table of Contents:
Jagged Array In Java
It is an array of arrays where each element is, in turn, an array. A special feature of this type of array is that it is a Multidimensional array whose each element can have different sizes.
For Example, a two-dimensional array in Java is an array of single dimension array. In the case of a two-dimensional array, each one-dimensional array will have different columns.
Given below is a pictorial representation of a Jagged array.
From the above pictorial representation, we got an idea of how does it look. Above shown is a two-dimensional Jagged array. Each individual element of this array is a one-dimensional array that has varied sizes as shown above.
The first 1D array has 3 columns; the second row has 2 columns while the third has 4 columns.
Create & Initialize Jagged Array
While creating an array of arrays you only specify the first dimension that represents a number of rows in the array.
You can create a two-dimensional jagged array as follows:
int myarray[][] = new int[3][];
In the above declaration, a two-dimensional array is declared with three rows.
Once the array is declared, you can define it as a Jagged array as shown below:
myarray[1] = new int[2]; myarray[2] = new int[3]; myarray[3] = new int[4];
The first statement above indicates that the first row in the 2D array will have 2 columns. The second row will have 3 columns while the third row will have 4 columns thereby making it a Jagged array.
Once the array is created, you can initialize it with values. Note that if you don’t explicitly initialize this array (as in the above case), then it will take the default values as initial values depending on the data type of the array.
Alternatively, you can also initialize an array as follows:
int myarray[][] = new int[][]{ new int[] { 1, 2, 3 }; new int[] { 4, 5, 6, 7 }; new int[] { 8, 9 }; };
Yet another way of initializing a Jagged array is by omitting the first new operator as shown below:
int[][]myarray ={ new int[] { 1, 2, 3 }; new int[] { 4, 5, 6, 7 }; new int[] { 8, 9 }; };
As you can see above, the new operator is omitted and the array is initialized as well as declared in the same statement.
You can also omit all the new operators altogether and have a declaration and initialization statement as shown below.
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9 } };
The program below initializes a ragged array by assigning initial values to each row. Here each row of the array is initialized to the column values.
class Main { public static void main(String[] args) { // Declare a 2-D array with 3 rows int myarray[][] = new int[3][]; // define and initialize jagged array myarray[0] = new int[]{1,2,3}; myarray[1] = new int[]{4,5}; myarray[2] = new int[]{6,7,8,9,10}; // display the jagged array System.out.println("Two dimensional Jagged Array:"); for (int i=0; i<myarray.length; i++) { for (int j=0; j<myarray[i].length; j++) System.out.print(myarray[i][j] + " "); System.out.println(); } } }
Output:
As shown in the output, the first row of Jagged array has 3 columns, the second row has 2 columns and the third row has 5 columns.
Jagged Array Example
Given below is an example of a Jagged array in Java. Here the array is initialized using for loops.
class Main { public static void main(String[] args) { // Declaring 2-D array with 4 rows int intArray[][] = new int[4][]; // create a jagged array intArray[0] = new int[3]; intArray[1] = new int[2]; intArray[2] = new int[1]; intArray[3] = new int[4]; // Initializing array with values for (int i=0; i<intArray.length; i++) for(int j=0; j<intArray[i].length; j++) intArray[i][j] = (i+1) * (j+1); //initial values for each row,column // display the contents of 2-D jagged array System.out.println("Two-dimensional Jagged Array:"); for (int i=0; i<intArray.length; i++) { for (int j=0; j<intArray[i].length; j++) System.out.print(intArray[i][j] + " "); System.out.println(); } } }
Output:
The above program defines a Jagged array of 4 rows. The column numbers of each row are then defined thereby creating an array of arrays. Then using for loops that traverse both rows and columns, the initial values are assigned to this array. The array is then printed using for loops.
Let’s implement another example of Ragged/Jagged arrays. In this program, we create a Jagged array in such a way that the ith row has i number of columns. This means that for row #1 the number of column(s) will be 1, row #2 will have 2 columns, row #3 will have 3 columns and so on.
class Main { public static void main(String[] args) { // Declare a 2-D array with 5 rows int intArray[][] = new int[5][]; // create a jagged array that has i column(s) for ith row for (int i=0; i<intArray.length; i++) intArray[i] = new int[i+1]; // Initialize the jagged array int count = 0; for (int i=0; i<intArray.length; i++) for(int j=0; j<intArray[i].length; j++) intArray[i][j] = count++; // Display the values of 2D Jagged array System.out.println("A two-dimensional Jagged Array contents:"); for (int i=0; i<intArray.length; i++) { for (int j=0; j<intArray[i].length; j++) System.out.print(intArray[i][j] + " "); System.out.println(); } } }
Output:
The above program output shows that each row has the number of columns equal to the corresponding row number. The elements are initialized to a sequence starting from 0.
Frequently Asked Questions
Q #1) What is the Jagged array in Java?
Answer: It is an array of arrays. It is a multi-dimensional array in which each element of this array which is another array is of different sizes. Hence we can create a two-dimensional Jagged array that has different sized columns.
Q #2) What is the use of a Jagged array?
Answer: Jagged arrays having multiple dimensions can improve the performance considerably.
Q #3) What is an Array of arrays called?
Answer: An array of arrays is the multidimensional array and is usually represented in the form of a matrix.
Q #4) Where are Multidimensional arrays used?
Answer: Multidimensional arrays including jagged arrays can be used to store information that requires a matrix form like time table, schedules, floor plans, etc.
Conclusion
In this tutorial, we discussed Jagged arrays which are Multidimensional arrays with varied sizes of columns in each row. These arrays are useful for improving the performance of the system.
They can be initialized either with the declaration itself or by using for loops. These arrays are also known as Ragged arrays and an Array of arrays.
=> Read Through The Easy Java Training Series.