0

How to print a Java array?

Simple Array

String[] array = new String[] {"John", "Mary", "Bob"};
System.out.println(Arrays.toString(array)); // [John, Mary, Bob]

Nested Array

String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
System.out.println(Arrays.toString(deepArray));
//output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
System.out.println(Arrays.deepToString(deepArray)); // [[John, Mary], [Alice, Bob]]

double Array:

double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
System.out.println(Arrays.toString(doubleArray)); // [7.0, 9.0, 5.0, 1.0, 3.0 ]

int Array

int[] intArray = { 7, 9, 5, 1, 3 };
System.out.println(Arrays.toString(intArray)); // [7, 9, 5, 1, 3 ]
add comment
Suggest a different solution or add another example