Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you’re asking.
Java 8 – Stream.forEach
1 2 |
int[] ints = { 3, 1, 8, 2, 7 }; IntStream.of(ints).forEach(System.out::println); |
Output:
1 2 3 4 5 |
3 1 8 2 7 |
Printing simple array
1 2 |
String[] array = new String[] {"Google", "Twitter", "Facebook"}; System.out.println(Arrays.toString(array)); |
Printing multidimensional array
1 2 3 4 |
String[][] deepArray = new String[][] {{"Google", "Facebook"}, {"Twitter", "Netflix"}}; System.out.println(Arrays.toString(deepArray)); //output: [[Ljava.lang.String;@106689c, [Ljava.lang.String;@52e934] System.out.println(Arrays.deepToString(deepArray)); |
Output:
1 |
[[Google, Facebook], [Twitter, Netflix]] |
Printing double Array
1 2 |
double[] doubleArray = { 3.0, 4.0, 1.0, 1.0, 7.0 }; System.out.println(Arrays.toString(doubleArray)); |
Output:
1 |
[3.0, 4.0, 1.0, 1.0, 7.0 ] |
int Array
1 2 |
int[] intArray = { 3, 1, 8, 2, 7 }; System.out.println(Arrays.toString(intArray)); |
Output:
1 |
[3, 1, 8, 2, 7 ] |