How to print a 2d array in java


In Java, arrays can be single-dimensional, 2-dimensional, or multi-dimensional. Java’s two-dimensional arrays are arrays within some other arrays. The 2D arrays are also known as matrices and they keep the data in the form of a table i.e. columns and rows. A 2D array can be created by specifying a data type followed by an array name and two sets of square brackets. In java, there are multiple ways to print a 2D array such as using for-each loop, for-loop, etc.

In this write-up, we will explain how to use the below-listed approaches to print a 2d array in Java:

So, let’s get started!

How to use nested for-loop to print a 2d array in Java?

The below-given example will guide you how to use nested for-loop to print a 2D array in java:

public class ExampleClass {
    public static void printArray(String array[][]) {

        for (int row = 0; row < array.length; row++)
        {
            for (int ind = 0; ind < array[row].length; ind++) {
                System.out.print(array[row][ind] + » «);
            }
            System.out.println();
        }
    }

    public static void main(String args[]) throws IOException {
        String originalArray[][] = {
            {«John», «Joe», «Mike»},
            {«Shaun», «Alex», «Henry»},
            {«Williams», «Dean», «Seth», «Ambrose»}};
        printArray(originalArray);
    }
}

Firstly, we created a printArray() method that takes a 2D array as an argument. Within the printArray() method, we utilized the nested for-loop to traverse through all the elements of the given array.

In the main method, firstly, we created a 2D string-type array and afterward we invoked the printArray() method:

The output verified that the nested for-loop successfully printed all the elements of the 2dimensional array.

How to use the for-each loop to print a 2-dimensional array?

Another way to print a 2-dimensional array is use of for-each loop. Let’s consider the following code block to learn how to print a 2D array in Java:

public class ExampleClass {
    public static void printArray(String array[][]) {
        for (String[] traverseRow : array) {
            for (String ind : traverseRow) {
                System.out.print(ind + «,»);
            }
            System.out.println();
        }
    }
    public static void main(String args[]) throws IOException {
        String originalArray[][] = {
            {«John», «Joe», «Mike», «Ambrose»},
            {«Shaun», «Alex», «Henry»},
            {«Williams», «Dean», «Seth»}};
        printArray(originalArray);
    }
}

This example remained the same as the previous one. The only difference is that, this time we utilized the foreach loop instead of for-loop:

This is how we can utilize the for-each loop to print a 2-dimensional array in java.

How to use Arrays.deepToString() to print a 2-dimensional array in Java?

A 2-dimensional array can be converted into a string using Java’s Arrays.deepToString() method. In the below-given code block, we’ll demonstrate how to print a 2-dimensional array in Java using the Arrays.ToString() method:

int[][] originalArray = {{12, 17, 18, 27}, {19, 44, 13, 18}};
System.out.println(Arrays.deepToString(originalArray));

In this coding example, initially, we created an integer-type 2-dimensional array. After that, we printed the 2D array using the “Arrays.deepToString()” method. The detailed code and output will be shown in the following snippet:

The above snippet shows that “Arrays.deepToString()” successfully printed the elements of the 2d array.

Conclusion

Java provides multiple ways to print a 2d array, for example nested for-loop, for-each loop, Arrays.deepToString() method, etc. Each approach follows a different procedure, but all of them can still accomplish the same goal, i.e., printing a 2D array. A couple of suitable examples were provided in this post to illustrate how to print a 2d array in Java using various techniques.



Source link