An array is a data structure to store multiple elements of similar data types. Similar to other programming languages Java also supports Arrays. Which are stored in a contiguous location on memory. In this tutorial, you’ll learn different techniques to print the elements of a given array in Java.

    1. Arrays.toString() method
    2. Arrays.deepToString() method
    3. for Loop
    4. for-each Loop
    5. Arrays.asList() method

Let’s discuss above methods one by one including examples.

1. Java Arrays.toString() Method

The Arrays.toString() method is the simplest and frequently used method to print an Array in Java programming.

import java.util.Arrays;

public class ArrayExample1 {

    public static void main(String[] args) {

        // Initialize an array

        String[] array = {“apple”,“orange”,“banana”,“grapes”,“mango”};

        // Print array elements

        System.out.println(Arrays.toString(array));

    }

}

Save above java program in ArrayExample1.java file and compile it and run. You will see the output like below:

Output:

[apple, orange, banana, grapes, mango]

2. Using Arrays.deepToString() Method

Java Arrays.deepToString() method is used to fetch the deep content from an array. Here the deep content represents to the multidimensional arrays.

import java.util.Arrays;

public class ArrayExample2 {

    public static void main(String[] args) {

        //declaration and initialization of a multidimensional array  

        int[][] array = {{1,2,3}, {4, 5}, {6, 7, 8}};

        // Print array elements

        System.out.println(Arrays.deepToString(array));

    }

}

Save above java program in ArrayExample2.java file and compile it and run. You will see the below output:

Output:

[[1, 2, 3], [4, 5], [6, 7, 8]]

3. Using for Loop

The for loop is an frequently used method used for integrations. We can navigate though all array elements by the array index number. The following example will print array elements using for loop in Java.

public class ArrayExample3 {

    public static void main(String[] args) {

        //declaration and initialization of an array

        String[] array = {“apple”,“orange”,“banana”,“grapes”,“mango”};

        // Print array elements

        for(int i=0;i<array.length;i )

            System.out.println(array[i]);  

    }

}

Save above java program in ArrayExample3.java file and compile it and run. You will see the below output:

Output:

apple orange banana grapes mango

4. Using for-each Loop

For-each is another way for traversing an array than for loop. Here is an quick example of using for-each to get array elements and print them.

public class ArrayExample4 {

    public static void main(String[] args) {

        //declaration and initialization of an array

        String[] array = {“apple”,“orange”,“banana”,“grapes”,“mango”};

        // Print array elements

        for (String element: array) {

            System.out.println(element);

        }

    }

}

Save program in a file named ArrayExample4.java. Then compile the program and run it. You will see the below output:

Output:

apple orange banana grapes mango

5. Using Arrays.asList() Method

The asList() is another java function that return a fixed-size list of an specified array.

import java.util.Arrays;  

public class ArrayExample5

{

    public static void main(String [] args)  

    {

        //declaration and initialization of an array

        String[] array = {“apple”,“orange”,“banana”,“grapes”,“mango”};

        // Print array elements

        System.out.println(Arrays.asList(array));

    }

}

Save java program to file named ArrayExample5.java, then compile and run it. You will see the below output:

Output:

[apple, orange, banana, grapes, mango]

Wrap Up

In this tutorial, you have learned 5 Java methods to print an array including example.