# Java Programs
➤ Array in Java
➤ Multidimensional Array
➤ Anonymous Array
➤ Array of Objects
➤ Jagged Array
➤ System.arraycopy()
# Java Array Programs
Java Arrays Class
➤ Arrays Class & Methods
➤ Arrays.toString()
➤ Arrays.sort() Method
➤ Arrays.copyof()
➤ Arrays.copyOfRange()
➤ Arrays.fill() Method
➤ Arrays.equals()
Others
➤ How to get Array Input
➤ Return Array in Java
➤ 2D Array in Java
➤ 3D Array in Java
➤ Matrix in Java
➤ Array of String in Java
➤ Array of double in Java
A multi-dimensional array with different sizes child array is called a Jagged array. It creates a table with different sizes of columns in a row. To create a jagged array, in multidimensional array creation we must not specify child array size instead we must assign child array objects with different sizes. Here you will learn Jagged Array in Java with Example.
This image represents an jagged array.
Jagged Array Creation in Java
1) Jagged array creation in Java with explicit values.
We can directly create jagged array with values. Example:- int[][] arr = {{10,9,8},{7,5,6,88},{30,15},{50},{10,20,30,40,50}};
2) Jagged array object creation in Java without explicit values or with default values.
To create a jagged array without explicit values, we need to use the new keyword. In this way of creation don’t mention child array size at the time of array declaration, leave it empty. Then in the next line initialize parent array locations with child array instances with different sizes.
int[][] arr = new int[5][];
// create the base array of size 5
Now we can create child array objects of any size.arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[4];
arr[3] = new int[1];
arr[4] = new int[3];
Java Program Example
class JaggedArray{
public static void main(String[] args) {
// declare and initialize array
int[][] arr = {
{10,9,8},
{7,5,6,88},
{30,15},
{50},
{10,20,30,40,50}
};
// displaying jagged array in Java
// using for-each loop
System.out.println("Jagged array is (using for-each loop):");
for(int[] i: arr){
for(int j : i){
System.out.printf("%d\t",j);
}
System.out.println();
}
// displaying jagged array in Java
// using for loop and length property
System.out.println("\nJagged array is (using for loop):");
for(int i=0; i < arr.length; i++){
for(int j=0; j < arr[i].length; j++){
System.out.printf("%d\t",arr[i][j]);
}
System.out.println();
}
}
}
Output:-
Jagged array is (using for-each loop):
10 9 8
7 5 6 88
30 15
50
10 20 30 40 50
Jagged array is (using for loop):
10 9 8
7 5 6 88
30 15
50
10 20 30 40 50
In this program, we used length property to find the size of each parent and child array. Here, arr[i].length gives the size of the child array whose parent is “i”, so for each parent, the i
will be different and it will give the valid size of each child array.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Gracias por compartir.
Very good explanation on Jagged array in Java.