【Java】Arrays.deepToString()与Arrays.toString()的区别

Arrays.deepToString()与Arrays.toString()的区别

  • Arrays.deepToString()主要用于数组中还有数组的情况;用于打印输出多维数组。
  • Arrays.toString()在当数组中有数组时,不会打印出数组中的内容,只会以地址的形式打印出来;用于打印输出一维数组。
public static void main(String[] args) {

        // int 类型
        int []a = {1, 2, 3};
        System.out.println(Arrays.toString(a));

        int [][]b = {{1, 2, 3}, {4, 5, 6}};
        System.out.println(Arrays.toString(b));
        System.out.println(Arrays.deepToString(b));

        System.out.println("-------------------");

        // String类型
        String []str1={"goog","le"};
        System.out.println(Arrays.toString(str1));

        String [][]str2={{"goog","le"},{"Jay","chou"}};
        System.out.println(Arrays.toString(str2));
        System.out.println(Arrays.deepToString(str2));
    }

结果:

[1, 2, 3]
[[I@4554617c, [I@74a14482]
[[1, 2, 3], [4, 5, 6]]
-------------------
[goog, le]
[[Ljava.lang.String;@677327b6, [Ljava.lang.String;@14ae5a5]
[[goog, le], [Jay, chou]]

你可能感兴趣的:(Java,java,Arrays)