What is the best way to print multidimensional arrays in Java?
•
Java
I recently started the mutltidimensional int array in Java Until then, one - dimensional arrays are enough
Print those I've used out. println(Arrays.toString(myarray));
However, it does not apply to multidimensional arrays. It has the same effect as when you try to print one-dimensional arrays directly multiple times
Solution
You need to iterate it
The easiest way is to use the provided arrays Deeptostring() to search the array in the array Notice that it works with arrays Like toString (), it is only available in Java 1.5, but I certainly want you to use Java 5. 0 now
For example:
int[][] myArray = { { 1,2,3 },{ 4,5,6 } }; System.out.println(Arrays.deepToString(myArray));
Alternatively, you can iterate on your own, or you can use the style you choose to print for more customization
For example:
int[][] myArray = { { 1,6 } }; for(int[] arr : myArray) System.out.println(Arrays.toString(arr));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码