Java array output
We usually use the for loop to output elements in an array, for example:
Package test; public class Test {public static void main (String args []) {int arr [] = {1, 2, 3}; System. out. print ("["); for (int I = 0; the output result of I is: [1, 2, 3].
However, Java provides a simpler method, which is the toString method. The procedure is as follows:
package test;import java.util.Arrays;public class Test {public static void main(String args[]){int arr[]={1,2,3};System.out.println(Arrays.toString(arr));}}The output result is: [1, 2, 3].
But what if the elements in the array are not of the same type? For example:
package test;import java.util.Arrays;public class Test {public static void main(String args[]){int[] arr={1,2,3};String[] str={"aaa","bbb"}; Object[] array = {arr,str};System.out.println(Arrays.toString(array));}}The output result is [[I @ 158f9d3, [Ljava. lang. String; @ 79a2e7].
The output is not an array element, but the memory code of the object. What if I want to print out the array elements? Here we can use deepToString. For example:
package test;import java.util.Arrays;public class Test {public static void main(String args[]){int[] arr={1,2,3};String[] str={"aaa","bbb"}; Object[] array = {arr,str};System.out.println(Arrays.deepToString(array));}}Output result: [[1, 2, 3], [aaa, bbb].