Print Array in Java Print Array in Java

Page content

In this tutorial, we’ll learn how to print the elements of a given Array in Java.

Simplest way

The Arrays.toString() and Arrays.deepToString() methods are the simplest way to print Arrays in Java and work well for all type of Arrays i.e. int, double, byte, String, etc.

Quick examples
System.out.println(Arrays.toString(new int[]{1, 2, 3}));
// prints [1, 2, 3]

System.out.println(Arrays.deepToString(new int[][]{{1, 2}, {3, 4}, {5, 6}}));
// prints [[1, 2], [3, 4], [5, 6]]

System.out.println(Arrays.deepToString(new int[][][]{{{1, 2}, {3}, {4}}, {{5, 6, 7}, {8}}, {{9, 10}}}));
// prints [[[1, 2], [3], [4]], [[5, 6, 7], [8]], [[9, 10]]]

Arrays.toString() for simple Array

The Arrays.toString() method is the simplest way to print elements of an Array in a single line in Java:-

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

Limitation of Array.toString()

The Arrays.toString() method works well with simple Array but prints ClassName@hashCode for nested Arrays like this:-

String[][] nestedArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
// prints something like [[Ljava.lang.String;@566776ad, [Ljava.lang.String;@6108b2d7]

Nothing to worry about! We have Arrays.deepToString() to the rescue.

Arrays.deepToString() for nested Array

The Arrays.deepToString() method can be used to print all the nested elements of a multi-dimensional Array in a single line in Java:-

String[][] nestedArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
// prints [[John, Mary], [Alice, Bob]]

String[][][] deepNestedArray = new String[][][] {{{"John"}, {"Mary"}, {"Alice"}}, {{"Bob"}, {"Adam"}}, {{"Emily"}}};
// prints [[[John], [Mary], [Alice]], [[Bob], [Adam]], [[Emily]]]

Other ways

There are many other ways to print an Array in Java:-

  1. Use Arrays.toString() and Arrays.deepToString() methods to convert an Array to a comma-separated String and print.
  2. Use Arrays.asList() and List.of() methods to convert an Array to a List and use its predefined toString() method to print elements in a comma-separated String.
  3. Use String.join() method to join elements of a String Array by any delimiter of your choice and print in a single line.
  4. Use Java Streams to map elements of an Array to String, join by any delimiter of your choice, and print in a single line.
  5. Use forEach loop to iterate through elements of an Array and print each element in a new line.

Let’s look at the examples:-

// Primitive int
int[] ints = new int[] {1, 2, 3, 4, 5};
System.out.println(IntStream.of(ints).mapToObj(Integer::toString).collect(Collectors.joining(", ")));
System.out.println(IntStream.of(ints).boxed().map(Object::toString).collect(Collectors.joining(", ")));

// Object String
String[] strs = new String[] {"John", "Mary", "Bob"};
System.out.println(String.join(", ", strs));
System.out.println(Stream.of(strs).collect(Collectors.joining(", ")));

// Object Enum 
DayOfWeek [] days = { FRIDAY, MONDAY, TUESDAY };
System.out.println(Stream.of(days).map(Object::toString).collect(Collectors.joining(", ")));