Codementor Events

Multidimensional Arrays In Java

Published Jan 26, 2025
Multidimensional Arrays In Java

1. Introduction

A multidimensional array is an array of arrays. While the most used types are two-dimensional arrays, Java allows you to create N-dimension arrays (N>=2) as well. In this article, you will learn how to create and manipulate multidimensional arrays in Java.

2. What is an N-Dimensional Array ?

In a Nutshell, an N-dimensional array is a normal array in which each item is an array of size N-1. For instance, a two-dimension array is a one-dimension array in which each item is a one-dimension array. As such, N-dimension arrays follow the same rules as a one-dimensional array: declaration, initialization, creation, accessing items, etc.
Common use cases for this type of array are:

  • Implementing Matrices
  • Implementing graphs
  • Image processing
  • Financial applications

3. Declaration

To declare an N-dimension array, you follow the same rule as for the one-dimension array. However, instead of using one [], you will use multiple [] depending on the number of dimensions. Below is an example of a two-dimension array.

type[][] my2DArrayVariable;

Or

type my2DArrayVariable[][];

4. Initialization

Just like normal arrays, You may initialize an N-dimension array by using the new operator and providing the size of each dimension. Java supports both rectangular arrays (all rows have the same length) and jagged arrays(rows with different lengths).

4.1. Rectangular Arrays

        int[][] my2DIntArray = new int[2][2];
        String[][][] my3DArrayOfStrings = new String[2][1][3];
        Person[][] my2DObjectArray = new Person[4][4];

You may also initialize with values in this way:

        int[][] my2DIntArray = {{1,2},{3,4}};//int[2][2]
        String[][][] my3DArrayOfStrings = {{{"a","b","c"}},{{"d","e","f"}}};//String[2][1][3]

4.2. Jagged Arrays

A jagged array is an array in which the length of the items across the same dimension can vary. Below is how can create and manipulate jagged arrays:

        int[][] my2DIntJaggedArray = {{1},{3,4},{5,6,7}};//Declaring and initializing at ounce
        String[][][] my3DJaggedArrayOfStrings = new String[2][1][];//Declaring and initializing separately
        my3DJaggedArrayOfStrings[0][0] = new String[2];
        my3DJaggedArrayOfStrings[1][0] = new String[3];
        my3DJaggedArrayOfStrings[0][0][0] = "Hello";
        my3DJaggedArrayOfStrings[0][0][1] = "World";
        my3DJaggedArrayOfStrings[1][0][0] = "I'm";
        my3DJaggedArrayOfStrings[1][0][1] = "John";
        my3DJaggedArrayOfStrings[1][0][2] = "Doe";

Do note that you can declare and initialize such arrays at ounce or declare and initialize them separately.

5. Accessing an N-Dimension Array

It can be very confusing for beginners to properly access an item in a multidimensional array. The rule is just to remember that the first `[]' is for the first dimension, the second one for the second dimension, and so on.

        int[][] my2DIntArray = {{1,2},{3,4}};
        int[][] my2DIntJaggedArray = {{1},{3,4},{5,6,7}};
        int[] firstRow = my2DIntArray[0];//[1, 2]
        int[] secondRow = my2DIntArray[1];//[3, 4]
        int[] firstRowJagged = my2DIntJaggedArray[0];//[1]
        int[] secondRowJagged = my2DIntJaggedArray[1];//[3, 4]
        int[] thirdRowJagged = my2DIntJaggedArray[2];//[5, 6, 7]
        int firstValue = my2DIntArray[0][0];//1
        int lastValue = my2DIntArray[1][1];//4
        int firstValueJagged = my2DIntJaggedArray[0][0];//1
        int lastValueJagged = my2DIntJaggedArray[2][2];//7

6. Miscellaneous operations

6.1. Accessing the size

Given that a multidimensional array is simply an array of arrays; to access its size, you need to access the size of each of its dimensions using the length attribute.

  • myArray.length will return the length of the first dimension, i.e. the number of rows.
  • myArray[index].length will return the number of rows at the specified index.
        int[][] my2DIntArray = {{1,2},{3,4}};
        int[][] my2DIntJaggedArray = {{1},{3,4},{5,6,7}};
        String[][][] my3DArrayOfStrings = {{{"a","b","c"}},{{"d","e","f"}}};
        int firstDimensionSize = my2DIntArray.length;//2
        int secondDimensionSize = my2DIntArray[0].length;//2

        int firstDimensionSizeJagged = my2DIntJaggedArray.length;//3
        int secondDimensionRow1SizeJagged = my2DIntJaggedArray[0].length;//1
        int secondDimensionRow2SizeJagged = my2DIntJaggedArray[1].length;//2
        int secondDimensionRow3SizeJagged = my2DIntJaggedArray[2].length;//3

        int firstDimensionStringArraySize = my3DArrayOfStrings.length;//2
        int secondDimensionStringArraySize = my3DArrayOfStrings[0].length;//1
        int thirdDimensionStringArraySize = my3DArrayOfStrings[0][0].length;//3

6.2. Looping through an N-Dimension array

Just like normal arrays, you can use a simple for loop, an enhanced for loop, or a while loop. The only difference here is that you will use nested loops.

        int[][] my2DIntArray = {{1,2},{3,4}};
        int[][] my2DIntJaggedArray = {{1},{3,4},{5,6,7}};

        for(int i=0; i<my2DIntArray.length; i++){
            for(int j=0; j<my2DIntArray[i].length;j++){
                System.out.print(" "+my2DIntArray[i][j]);
            }
            System.out.println();
        }

        for(int i=0; i<my2DIntJaggedArray.length; i++){
            for(int j=0; j<my2DIntJaggedArray[i].length;j++){
                System.out.print(" "+my2DIntJaggedArray[i][j]);
            }
            System.out.println();
        }

Pro-tip: Always use array[index].length when looping through multidimensional arrays. Because Java allows jagged arrays, this prevents unexpected ArrayIndexOutOfBoundException.

6.3. Displaying the content

You can use loops like we saw in the previous section and display the items individually. Alternatively, you may use the Arrays Utility class.

        int[][] my2DIntArray = {{1,2},{3,4}};
        int[][] my2DIntJaggedArray = {{1},{3,4},{5,6,7}};

        System.out.println(Arrays.deepToString(my2DIntArray));
        System.out.println(Arrays.deepToString(my2DIntJaggedArray));

7. Conclusion

In this article, you learned how to manipulate multidimensional arrays in Java.
You can find the complete code of this article here in GitHub.

Originally published at https://nkamphoa.com

Discover and read more posts from Noel KAMPHOA
get started