Skip to content

5 Methods to Print an Array in Java

Category
Programming
Covers
Java
Versions & support
About Java →
On this page

An array is a data structure to store multiple elements of similar data types. Similar to other programming languages Java also supports Arrays. An Array is always stored in a contiguous location on the system memory. Java provides multiple methods of printing an Array basis on the requirements. We can directly access any array of elements with the index number or print the entire Array using Java loops.

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 the 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.

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 the multidimensional arrays.

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 a frequently used method used for integrations. We can navigate through all array elements by the array index number. The following example will print array elements using for loop in Java.

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 a quick example of using for-each to get array elements and print them.

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 returns a fixed-size list of a specified 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 examples.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top