Skip to content

Java Print Array Elements | Ways | Simple examples

  • by

There are many ways to get print Array Elements. Using a Loop statement is the most program used. Maybe some of you only knew for-loop. But in this tutorial, you will learn 3 ways to Java Print Array Content with examples.

Java Print Array Elements, Ways with Simple examples

Ways to Java Print Array

There are three ways to print an array.

  • Print an Array using For loop
  • Standard library static method – Arrays.toString(array)
  • Stream and lambda to print the array.

Note: When you tried to Print the Array element using the toString() method. You will get Output the className + @ + the hex of the hashCode of the array.

Java Print Array examples

Let’s start looking one by one for each method to get print data from the array in java. These all examples are for a one-dimensional array.

1. For loop

This can be your interview question like – How to print string array in java using for loop?

In Java, there are many loops like – for-loop, for-each, do-while, and while loop. For this example, we will use a for-loop. You can use other ones in the same way as below print string array in java using for loop.

public class Hello {

    public static void main(String[] args) {
        String[] array = {"Array", "Tutorial", "Example"};

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}

Output: Array
Tutorial
Example

Another example of int values or how to print int array.

Below an example using a for-each loop to iterate over the given array,

public class Hello {

    public static void main(String[] args) {
        int[] array = {7, 6, 4};

        for (int element : array) {
            System.out.println(element);
        }
    }
}

Output: 7
6
4

2. Standard library - Arrays.toString(array)

You need to import a java.util.Arrays package for that example. You don't need to run in a loop for it, just one line code - Arrays.toString(array) required. But in the output, you can see the different, above example simple print the array element but using Arrays.toString(array) print as an array.

import java.util.Arrays;

public class Hello {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

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

Output: [1, 2, 3, 4, 5]

3. Stream and lambda to print the array

Prior to Java 8 - you can use Arrays.toString(array) to print a one-dimensional array and Arrays.deepToString(array) for multi-dimensional arrays.

Java 8 - Have a new way of Stream and lambda to print the array. See the below examples, how to use it.

Printing One dimensional Array:

import java.util.Arrays;

public class Hello {

    public static void main(String[] args) {
        int[] intArray = new int[] {1, 2, 5};
        String[] strArray = new String[] {"AB", "CD", "EF"};

        // In Java 8 we have lambda expressions
        Arrays.stream(intArray).forEach(System.out::println);
        Arrays.stream(strArray).forEach(System.out::println);
    }
}

Output: 1
2
5
AB
CD
EF

Printing Multi-dimensional Array

import java.util.Arrays;

public class Hello {

    public static void main(String[] args) {
        int[][] int2DArray = new int[][]{{11, 12}, {31, 32, 33}};
        String[][] str2DArray = new String[][]{{"A", "B"}, {"C", "D"}};


        // In Java 8 we have lambda expressions
        Arrays.stream(int2DArray).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);
        Arrays.stream(str2DArray).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);
    }
}

Output: 11
12
31
32
33
A
B
C
D

How to Print a Multi-dimenstional Array?

Print 2D array or Nested Array in java needed deepToString() method. See below example - print elements of array java. This function works for  3-dimensional arrays as well.

import java.util.Arrays;

public class Hello {

    public static void main(String[] args) {
        int[][] array = {{1, 2}, {3, 4}, {5, 6, 7}};

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

Output: [[1, 2], [3, 4], [5, 6, 7]]

double Array Example:

double[] doubleArray = { 5.0, 2.0, 5.0, 1.0, 3.0 };
System.out.println(Arrays.toString(doubleArray));

Output: [5.0, 2.0, 5.0, 1.0, 3.0 ]

int Array Example:

int[] intArray = { 3, 7, 5, 1, 3 };
System.out.println(Arrays.toString(intArray));

Output: [3, 7, 5, 1, 3 ]

Question: How to print array in java without loop?

Answer: You can use a Standard library static method - Arrays.toString(array) and Stream and lambda to print the array. This method not required the use of a loop statement.

Do comment if you have any doubts and suggestions or examples.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java Print Array Elements is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading