Print an Array in Java

This is one of the most frequently asked questions for Java programmers. The following shows 5 different ways of printing an array in Java.

5 Different Ways of Printing an Array

for(int n: arr) 
   System.out.println(n+", ");
for (int i = 0; i < arr.length; i++) {
   System.out.print(arr[i] + ", ");
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 other arrays as elements, we can use Arrays.deepToString() to print the array:

Category >> Array  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";