Print an Array in Java

This post summarizes 5 different ways of printing an array in Java.

5 Different Ways of Printing an Array

Arrays.toString(arr)
for(int n: arr) 
   System.out.println(n+", ");
for (int i = 0; i < arr.length; i++) {
   System.out.print(arr[i] + ", ");
}
System.out.println(Arrays.asList(arr));
Arrays.asList(arr).stream().forEach(s -> System.out.println(s));

The Winner

Apparently, the first one is simple enough. If an array is multi-dimensional, i.e., it contains arrays as elements, we can use Arrays.deepToString() to print the array:

System.out.println(Arrays.deepToString(arr));

Leave a Comment