Java

How to create multidimensional arrays in java

Arrays are one of the fundamental and most frequently used concepts in any programming language. Arrays in java are data structures used to store the data of similar data types in consecutive memory locations. The significance of an array is that it can hold multiple values of a similar data type in a single variable/memory location.

In java, arrays can be either single-dimensional or multidimensional arrays. In this post, we will learn the basics of multidimensional arrays. So, without a further delay, let’s begin!

What are multidimensional Arrays in Java?

Multidimensional Arrays, it’s a combination of two words “multi” and “dimensions” so, we can say that an array consisting of more than one dimension is known as a multidimensional array in java. The data in a multidimensional array will be kept in a tabular form i.e. columns and rows. In simple words, we can say that it is an array of arrays.

How to Create Multidimensional Arrays in Java?

There are multiple ways to create a multidimensional array in Java, some of them are discussed below:

dataType[][] user-defined arrayName;

The above snippet shows that first comes the data type like int, float, string, etc. followed by two sets of square brackets [][] and finally comes the array name.

dataType arrayName[][];

We can shuffle the array name and square brackets i.e. specify the array name before the square brackets.

Another way of creating a multidimensional array is shown in the below-given snippet:

dataType []arrayName[];

How to calculate the size of multidimensional Arrays?

In java, we can find the size of a multidimensional array by multiplying the number of rows by the number of columns.

How to Instantiate Multidimensional Arrays in Java?

The below code snippet shows the basic syntax of instantiating the multidimensional arrays in Java:

dataType arrayName[][] = new dataType[rows][columns];

The above snippet shows that to instantiate a Multidimensional array in java, we have to utilize the new operator, followed by data type and finally, the number of rows and number of columns will be specified within the square brackets.

How to Initialize Multidimensional Arrays in Java?

The below given code block shows the basic syntax of initializing a multidimensional array in java:

arrayName[rowIndex][columnIndex = User-specified value;

The below snippet explains how to create and Initialize Multidimensional Arrays in one step:

dataType arrayName[][]= { {val1, val2, val3, val4}, {val5, val6, val7}, ... };

Practical Demonstration of Multidimensional Arrays in Java

As of now, we have learned how to create/declare, instantiate and initialize a multidimensional array in Java. Now we will implement all these concepts practically.

How to create, initialize, and print a two-dimensional array in Java?
In this example program we will create a string type two-dimensional array in java:

public class MultidimensionalArray {
    public static void main(String[] args) {
        String names[][] = {{"Mike", "Joe", "John"}, {"Seth", "Bryn", "Paul"},
        {"Clarke", "Denly", "Ambrose"}};

        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                System.out.print(names[r][c] + " ");
            }
            System.out.println();
        }
    }
}

In the above given java program, firstly, we created a multidimensional array named “names” and initialized it with some string values. Next, we utilized the nested for-loop to access and print the values of that array:

The output verified the working of the above-given java program.

How to create, initialize, and print a three-dimensional array in Java?
In this example we will learn how to create a string type three-dimensional array in java:

public class MultidimensionalArray {

    public static void main(String[] args) {

        String names[][][] = {{
            {"Mike", "Joe", "John"},
            {"Seth", "Bryn", "Paul"},
            {"Clarke", "Denly", "Ambrose"}
        }
        };

        for (int x = 0; x <1; x++) {
            for (int y = 0; y <= 2; y++) {
                for (int z = 0; z <= 2; z++) {
                    System.out.print(names[x][y][z] + " ");
                }
                System.out.println();
            }
        }
    }
}

In the above program, firstly we created a three dimensional array and afterward we utilized three nested for loops to access and print the three-dimensional array.

This is how multidimensional arrays work in Java.

Conclusion

In java, there are multiple ways to create a multidimensional array, among them the most common way of creating a multidimensional array is to specify the data type like int, float, string, etc. followed by two sets of square brackets [][] and finally comes the array name. This write-up explained how to create/declare, instantiate and initialize a multidimensional array in Java with the help of some suitable examples. Descriptive screenshots are also provided for a profound understanding of the concepts.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.