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
Let us look at two quick ways of printing the elements, instead of the internal representation, of an array using -
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
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 -
Arrays.toString()andArrays.deepToString()methods.Arrays.stream()andStream.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));
}
}
[1, 4, 8, 10, 6][su_spacer size="1"] [[20, 30, 45], [55, 2, 26]]
oneDimensionalint arrayis passed toArrays.toStringmethod, which returns aStringified representation of the array elements between square brackets and separated by commas.twoDimensionalint arrayis passed toArrays.deepToStringmethod, which returns aStringified 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);
}
}
1 4 8 10 6 John David Joe Frank 20 30 45 55 2 26
oneDimensionalint array is passed toArrays.stream()method which creates aStreamout of the array elements. The elements in the stream are then printed usingforEach()method to which a method reference to theSystem.out.println()method is passed.two_dim_Strwhich is a two dimensionalString arrayis passed toArrays.stream()method which creates aStreamofStringarrays. Then theflatMap()method is used to flatten eacharrayinstance in the stream into stream ofStringelements in that instance. This flattened stream ofStrings is then printed usingforEach()method to which a method reference to theSystem.out.println()method is passed.two_dim_intwhich is a two dimensionalint arrayis passed toArrays.stream()method which creates aStreamof int arrays. Then theflatMapToInt()method is used to flatten each array instance in the stream into stream ofintelements in that instance. This flattened stream ofintelements is then printed usingforEach()method to which a method reference to theSystem.out.println()method is passed.