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.
Examples:
Simple Array:
String[] array = new String[] {"John", "Mary", "Bob"}; System.out.println(Arrays.toString(array));Output:
[John, Mary, Bob]Nested Array:
String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}}; // Gives undesired output: System.out.println(Arrays.toString(deepArray)); // Gives the desired output: System.out.println(Arrays.deepToString(deepArray));Output:
[[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] [[John, Mary], [Alice, Bob]]doubleArray:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]intArray:int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
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.
Examples:
Simple Array:
String[] array = new String[] {"John", "Mary", "Bob"}; System.out.println(Arrays.toString(array));Output:
[John, Mary, Bob]Nested Array:
String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}}; // Gives undesired output: System.out.println(Arrays.toString(deepArray)); // Gives the desired output: System.out.println(Arrays.deepToString(deepArray));Output:
[[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] [[John, Mary], [Alice, Bob]]doubleArray:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]intArray:int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
Always check the standard libraries first.
import java.util.Arrays;
Then try:
System.out.println(Arrays.toString(array));
or if your array contains other arrays as elements:
System.out.println(Arrays.deepToString(array));
Videos
List<int[]>[] adj = new ArrayList[n + 1];
I tried this
System.out.println(Arrays.deepToString(adj));
and it prints [[[I@20ad9418], [[I@31cefde0], [[I@439f5b3d], [], []]
- This is how I am expecting the result to be [ [], [], [[1, 1], [3, 1]], [[4, 1]], []]
- It's okay if the format isn't as it is but the below would also work
idx 0: [] idx 1: [] idx 2: [1, 1], [3, 1] idx 3: [4, 1] idx 4: []
Edit:
Thank you all, for your wisdom words, I am able to print using both nested for loops and streams. And I am commenting so that it will be helpful for future learners :)
// Print array of lists of arrays Using regular for loops
for (int i = 0; i < adj.length; i++) {
System.out.print("idx " + i + ": ");
for (int j = 0; j < adj[i].size(); j++) {
System.out.print(Arrays.toString(adj[i].get(j)));
}
System.out.println();
}// Print array of lists of arrays Using Stream
Arrays.stream(adj).forEach(lst -> {
System.out.println(Arrays.deepToString(lst.toArray()));
});