Jagged array in Java

  • Last Updated: December 29, 2023
  • By: javahandson
  • Series
img

Jagged array in Java

In this article, we will learn what is a jagged array in Java with proper examples. If you want to learn more about other Java, Java 8, or Spring concepts then please check the above menu options.

 

A jagged array is a group of arrays. It means we can add multiple arrays to a single array. A jagged array can store a 1D array or multi-dimensional array and the stored arrays can be of any size.

It’s quite difficult to understand the above definition hence we will try to understand a jagged array by comparing it with a 2D array. In a 2D array, we know the exact number of rows and columns. We will initialize a 2D array like below.

int numbers[][] = new int[4][3];

The numbers array contains 4 rows and 3 columns. So the rows and columns are fixed here we cannot change them at runtime. The numbers array will look something like below.

101 102 103
201 202 203
301 302 303
401 402 403

Contrary to the above a jagged array will have a fixed number of rows but it will not have a fixed number of columns. For each row, the number of columns can change. The jagged array can look something like below.

101 102 103
201 202 203 204
301 302 
401 402 403

The above 2D jagged array contains four 1D arrays of different sizes.

The first 1D array is having 3 elements
The second 1D array is having 4 elements.
The third 1D array is having 2 elements.
The fourth 1D array is having 3 elements.

Note* We can also provide the number of columns dynamically at runtime to a jagged array.

Creating a 2D Jagged array

There are 2 ways of creating a 2D jagged array:

1. We can declare a 2D jagged array and store the values of elements directly at the time of declaration.

int numbers[][] = {{101, 102, 103}, {201, 202, 203, 204}, {301, 302}, {401, 402, 403}};
  • int is the data type of the above 2D jagged array.
  • Two square brackets are used to declare a 2D jagged array. The square brackets can be used before or after the array name.
  • Each row of elements should be written inside the curly braces.
  • The rows and the elements in each row should be separated by commas.
package com.java.handson.arrays;

public class JaggedArrays {
    public static void main(String[] args) {

        int numbers[][] = {{101, 102, 103}, {201, 202, 203, 204}, {301, 302}, {401, 402, 403}};

        System.out.println("3rd element of first row : "+numbers[0][2]);
        System.out.println("3rd element of second row : "+numbers[1][2]);
        System.out.println("3rd element of fourth row : "+numbers[3][2]);
    }
}
Output:
3rd element of first row : 103
3rd element of second row : 203
3rd element of fourth row : 403

Jagged array ArrayIndexOutOfBoundsException

As we know a jagged array is a group of arrays. The numbers array is a collection of four 1D arrays of different sizes.

101 102 103
201 202 203 204
301 302 
401 402 403

The third array has only 2 elements. If we try to access a third element in that array then we will get an ArrayIndexOutOfBoundsException. Let us see the example below.

package com.java.handson.arrays;

public class JaggedArrays {
    public static void main(String[] args) {

        int numbers[][] = {{101, 102, 103}, {201, 202, 203, 204}, {301, 302}, {401, 402, 403}};

        System.out.println("3rd element of first row : "+numbers[0][2]);
        System.out.println("3rd element of second row : "+numbers[1][2]);
        System.out.println("3rd element of third row : "+numbers[2][2]);  // Exception is raised here
        System.out.println("3rd element of fourth row : "+numbers[3][2]);
    }
}
Output:
3rd element of first row : 103
3rd element of second row : 203
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
	at com.java.handson.arrays.JaggedArrays.main(JaggedArrays.java:11)

2. We can declare a 2D jagged array and just assign the number of rows to the array using a new operator. We will not assign the number of columns to the array at the time of declaration.

Or we can say that we will specify the number of 1D arrays but we will not specify the number of elements in each array.

int numbers[][] = new int[4][];

The above numbers array has 4 rows ( or four 1D arrays ) but the number of columns is not specified. If we have to specify the number of columns in each array then we can do that like below.

numbers[0] = new int[3]; // First array will have 3 elements.
numbers[1] = new int[4]; // Second array will have 4 elements.
numbers[2] = new int[2]; // Third array will have 2 elements.
numbers[3] = new int[3]; // Fourth array will have 3 elements. 
package com.java.handson.arrays;

public class JaggedArrays {
    public static void main(String[] args) {

        int numbers[][] = new int[4][];

        numbers[0] = new int[3];
        numbers[1] = new int[4];
        numbers[2] = new int[2];
        numbers[3] = new int[3];

        numbers[0][0] = 101;
        numbers[0][1] = 102;
        numbers[0][2] = 103;

        numbers[1][0] = 201;
        numbers[1][1] = 202;
        numbers[1][2] = 203;
        numbers[1][3] = 204;

        numbers[2][0] = 301;
        numbers[2][1] = 302;

        numbers[3][0] = 401;
        numbers[3][1] = 402;
        numbers[3][2] = 403;

        for (int i=0; i<4; i++) {
            for (int j=0; j<numbers[i].length; j++) {
                System.out.print(numbers[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Output:
101 102 103 
201 202 203 204 
301 302 
401 402 403 

Note* In the above example numbers[i].length gives the number of elements or columns in that array.

Creating a 3D Jagged array

There are 2 ways of creating a 3D jagged array:

1. We can declare a 3D jagged array and store the values of elements directly at the time of declaration.

int numbers[][][] = {{{101, 102, 103},{104, 105, 106}}, 
                    {{201, 202, 203, 204},{205, 206, 207, 208}, {209, 210, 211, 212}, {213, 214, 215, 216}},
                    {{301, 302}, {303, 304}, {305, 306}}};
  • int is the data type of the above 2D jagged array.
  • Three square brackets are used to declare a 3D jagged array. The square brackets can be used before or after the array name.

A 3D array comprises of rows, columns, and depth. Hence to access any element in a 3D jagged array we should know all the 3 dimensional positions.

package com.java.handson.arrays;

public class JaggedArrays {
    public static void main(String[] args) {

        int numbers[][][] = {{{101, 102, 103},{104, 105, 106}},
                {{201, 202, 203, 204},{205, 206, 207, 208}, {209, 210, 211, 212}, {213, 214, 215, 216}},
                {{301, 302}, {303, 304}, {305, 306}}};

        System.out.println("1st row 2nd column 3rd element : "+numbers[0][1][2]);
        System.out.println("2nd row 4th column 3rd element : "+numbers[1][3][2]);
        System.out.println("3rd row 3rd column 1st element : "+numbers[2][2][0]);
    }
}
Output:
1st row 2nd column 3rd element : 106
2nd row 4th column 3rd element : 215
3rd row 3rd column 1st element : 305

2. We can declare a 3D jagged array and just assign the number of rows to the array using a new operator. We will not assign the number of columns and depth to the array at the time of declaration.

A 3D array is a combination of multiple 2D arrays.

int numbers[][][] = new int[3][][];

The above numbers array will have three 2D arrays ( or 3 rows ). If we have to specify the number of columns and elements present in each 2D array then we can do that like below.

numbers[0] = new int[2][3]; // First row with 2 columns and 3 elements ( each column will have 3 elements )
numbers[1] = new int[4][4]; // Second row with 4 columns and 4 elements ( each column will have 4 elements )
numbers[2] = new int[3][2]; // Third row with 3 columns and 2 elements  ( each column will have 2 elements )
package com.java.handson.arrays;

public class JaggedArrays {
    public static void main(String[] args) {

        int numbers[][][] = new int[3][][];

        numbers[0] = new int[2][3];
        numbers[1] = new int[4][4];
        numbers[2] = new int[3][2];

        numbers[0][0][0] = 101;
        numbers[0][0][1] = 102;
        numbers[0][0][2] = 102;
        numbers[0][1][0] = 104;
        numbers[0][1][1] = 105;
        numbers[0][1][2] = 106;

        numbers[1][0][0] = 201;
        numbers[1][0][1] = 202;
        numbers[1][0][2] = 203;
        numbers[1][0][3] = 204;
        numbers[1][1][0] = 205;
        numbers[1][1][1] = 206;
        numbers[1][1][2] = 207;
        numbers[1][1][3] = 208;
        numbers[1][2][0] = 209;
        numbers[1][2][1] = 210;
        numbers[1][2][2] = 211;
        numbers[1][2][3] = 212;
        numbers[1][3][0] = 213;
        numbers[1][3][1] = 214;
        numbers[1][3][2] = 215;
        numbers[1][3][3] = 216;

        numbers[2][0][0] = 301;
        numbers[2][0][1] = 302;
        numbers[2][1][0] = 303;
        numbers[2][1][1] = 304;
        numbers[2][2][0] = 305;
        numbers[2][2][1] = 306;

        for (int row=0; row<3; row++) {
            for (int column=0; column<numbers[row].length; column++) {
                for (int depth=0; depth<numbers[row][column].length; depth++) {
                    System.out.print(numbers[row][column][depth]+" ");
                }
                System.out.print("   ");
            }
            System.out.println();
        }
    }
}
Output:
101 102 102    104 105 106    
201 202 203 204    205 206 207 208    209 210 211 212    213 214 215 216    
301 302    303 304    305 306

Note* In the above example numbers[row].length gives the number of columns in each row and numbers[row][column].length gives the number of elements in each column i.e. depth.

So this is all about jagged arrays in Java with proper examples. If you have any questions on this topic please raise them in the comments section. If you liked this article then please share this post with your friends and colleagues so that they can learn Java and its frameworks in more depth.

Leave a Comment