What’s the simplest way to print a Java array?

simplest way to print a Java array

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. Arrays are one of the most frequently used data type in Java.
As it holds multiple value we can not print it normally like we do with string or int. To print arrays we can try below methods:

Note : Please use "import java.util.Arrays" as we are using it to handle arrays.

Using Java 8

Print array using Java 8

String[] strArray = new String[] {"John", "Mary", "Bob"};

// #1
Arrays.asList(strArray).stream().forEach(s -> System.out.println(s));

// #2
Stream.of(strArray).forEach(System.out::println);

// #3
Arrays.stream(strArray).forEach(System.out::println);
John
Mary
Bob

Multi-dimensional Array Using Java 8

int[][] int2DArray = new int[][] { {11, 12}, { 21, 22}, {31, 32, 33} };
String[][] str2DArray = new String[][]{ {"John", "Bravo"} , {"Mary", "Lee"}, {"Bob", "Johnson"} };


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
21
22
31
32
33
John
Bravo
Mary
Lee
Bob
Johnson


Prior to Java 8

We can use toString() and deepToString() method of java.util.Arrays class.

public static String toString(Object[] a)


Returns a string representation of the contents of the specified array. If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.

The value returned by this method is equal to the value that would be returned by Arrays.asList(a).toString(), unless a is null, in which case "null" is returned.

Parameters:
a - the array whose string representation to return
Returns:
a string representation of a
public static String deepToString(Object[] a)

Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.

The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(Object), unless they are themselves arrays.

If an element e is an array of a primitive type, it is converted to a string as by invoking the appropriate overloading of Arrays.toString(e). If an element e is an array of a reference type, it is converted to a string as by invoking this method recursively.

To avoid infinite recursion, if the specified array contains itself as an element, or contains an indirect reference to itself through one or more levels of arrays, the self-reference is converted to the string "[...]". For example, an array containing only a reference to itself would be rendered as "[[...]]".

This method returns "null" if the specified array is null.


Parameters:
a - the array whose string representation to return
Returns:
a string representation of a

Simple Array

String[] array = new String[] {"coderboi", "java", "spring"};
System.out.println(Arrays.toString(array));

Output

[coderboi, java, spring]

Nested Array

String[][] nestedArray = new String[][] {{"Romeo", "Juliet"}, {"Antony", "Cleopatra"}};
System.out.println(Arrays.deepToString(nestedArray));

double Array

double[] doubleArray = { 8.0, 3.0, 3.4, 5.6, 9.9 };
System.out.println(Arrays.toString(doubleArray));
[8.0, 3.0, 3.4, 5.6, 9.9]

int Array

int[] intArray = { 17, 30, 54, 22, 23 };
System.out.println(Arrays.toString(intArray));

Output

[17, 30, 54, 22, 23]