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]]
oneDimensional
int array
is passed toArrays.toString
method, which returns aString
ified representation of the array elements between square brackets and separated by commas.twoDimensional
int array
is passed toArrays.deepToString
method, which returns aString
ified 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
oneDimensional
int array is passed toArrays.stream()
method which creates aStream
out 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_Str
which is a two dimensionalString array
is passed toArrays.stream()
method which creates aStream
ofString
arrays. Then theflatMap()
method is used to flatten eacharray
instance in the stream into stream ofString
elements in that instance. This flattened stream ofString
s is then printed usingforEach()
method to which a method reference to theSystem.out.println()
method is passed.two_dim_int
which is a two dimensionalint array
is passed toArrays.stream()
method which creates aStream
of int arrays. Then theflatMapToInt()
method is used to flatten each array instance in the stream into stream ofint
elements in that instance. This flattened stream ofint
elements is then printed usingforEach()
method to which a method reference to theSystem.out.println()
method is passed.