Java How To Print An Array
Printing an array in Java is easy. It might not necessarily seem intuitive because by default the println() method will just print the address of the array which is not what you want 99% of the time.
To print an array in Java just do this:
System.out.println( Arrays.toString(array1));
This might be a bit different if you have nested array or if you want to print line at a time.
Here is a longer example that shows how to print different types of Java arrays in different ways.
import java.util.*;
public class Test1 {
public static void main( String[] args ){
String[] a1 = { "one", "two", "three" };
int[] a2 = { 1, 2, 3 };
double[] a3 = { 1.2, 2.0, 3.15 };
String[][] a4 = { { "a", "b", "c" }, { "x", "y", "z" } };
// print entire array
System.out.println( Arrays.toString(a1));
System.out.println( Arrays.toString(a2));
System.out.println( Arrays.toString(a3));
System.out.println( Arrays.deepToString( a4 ));
// print element at a time
for( String i: a1 )
System.out.println( i );
for( int i: a2 )
System.out.println( i );
for( double i: a3 )
System.out.println( i );
for( String[] i: a4 ) {
for( String x: i )
System.out.print( x + " " );
System.out.println("");
}
}
}