How to Print array in Java using Arrays.toString, Arrays.deepToString, Arrays.stream methods

This quick coding tip shows how to print array in Java for both one dimensional and multi-dimensional arrays.

If you invoke System.out.println(<array-name>) on a single dimensional array variable named <array-name> what you will see as output is something like this – [I@404b9385 – and NOT the elements in the array. Similarly, if <array-name> was a 2-dimensional array then you would see something like this – [[I@682a0b20, [I@6d311334.

Let us look at two quick ways of printing the elements, instead of the internal representation, of an array using –

  1. Arrays.toString() and Arrays.deepToString() methods.
  2. Arrays.stream() and Stream.flatMap() methods introduced in Java 8.
Print array using Arrays.toString() and Arrays.deepToString()
package com.javabrahman.corejava;
import java.util.Arrays;
public class PrintArrayUsingToString {
  public static void main(String args[]) {
    int[] oneDimensional = {1, 4, 8, 10, 6};
    System.out.println(Arrays.toString(oneDimensional));

    int[][] twoDimensional={{20,30,45},{55,2,26}};
    System.out.println(Arrays.deepToString(twoDimensional));
  }
}
 OUTPUT of the above code
[1, 4, 8, 10, 6]
[[20, 30, 45], [55, 2, 26]]
Explanation of the code

  • oneDimensional int array is passed to Arrays.toString method, which returns a Stringified representation of the array elements between square brackets and separated by commas.
  • twoDimensional int array is passed to Arrays.deepToString method, which returns a Stringified representation of the array elements, with each dimension’s element array between square brackets and separated by commas. The whole array is then again enclosed between square brackets.
Print array using Arrays.stream(), Stream.flatMap() and Stream.flatMapToInt()
package com.javabrahman.corejava;
import java.util.Arrays;
public class PrintArrayUsingStreams {
  public static void main(String args[]) {
    int[] oneDimensional = {1, 4, 8, 10, 6};
    Arrays.stream(oneDimensional).forEach(System.out::println);

    String[][] two_dim_Str={{"John","David"},{"Joe","Frank"}};
    Arrays.stream(two_dim_Str)
           .flatMap(array -> Arrays.stream(array))
           .forEach(System.out::println);

    int[][] two_dim_int={{20,30,45},{55,2,26}};
    Arrays.stream(two_dim_int)
          .flatMapToInt(array->Arrays.stream(array))
          .forEach(System.out::println);
  }
}
 OUTPUT of the above code
1
4
8
10
6
John
David
Joe
Frank
20
30
45
55
2
26
Explanation of the code

  • oneDimensional int array is passed to Arrays.stream() method which creates a Stream out of the array elements. The elements in the stream are then printed using forEach() method to which a method reference to the System.out.println() method is passed.
  • two_dim_Str which is a two dimensional String array is passed to Arrays.stream() method which creates a Stream of String arrays. Then the flatMap() method is used to flatten each array instance in the stream into stream of String elements in that instance. This flattened stream of Strings is then printed using forEach() method to which a method reference to the System.out.println() method is passed.
  • two_dim_int which is a two dimensional int array is passed to Arrays.stream() method which creates a Stream of int arrays. Then the flatMapToInt() method is used to flatten each array instance in the stream into stream of int elements in that instance. This flattened stream of int elements is then printed using forEach() method to which a method reference to the System.out.println() method is passed.

Note – In case you are new to Java 8 and method references then you can understand them by reading the method reference tutorialRead Java 8 method reference tutorial.

 

Digiprove sealCopyright © 2014-2019 JavaBrahman.com, all rights reserved.