有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在使用数组作为变量时没有得到完整的数组

我是Java的新手,正在学习。 我有一个关于数组的问题,你能查一下吗

我得到以下输出:[[Ljava.lang.String;@7a982589] 代码如下:

String[][] multi = new String [][] {
{ "Scenarios", "Description", "1.0", "1.1", "1.2"},
{ "S1", "Verify hotel search", "Y", "Y", "Y"},
};
System.out.println(multi);

而如果我放置以下内容:

System.out.println(multi[0][1]);

我得到了正确的输出。 描述

现在,我为什么不通过“multi”变量得到整个数组


共 (6) 个答案

  1. # 1 楼答案

    系统。出来println调用对象参数上的toString来获取它们的文本表示。Java数组也是对象,它们从Java继承toString。lang.对象:

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    
  2. # 2 楼答案

    使用^{}。您看到的是默认的Object.toString(),因为数组不会覆盖toString()

  3. # 3 楼答案

    使用这个util方法,因为你有一个2D数组。阅读api Arrays#deepToString(Object[])

    Arrays.deepToString(multi);
    

    Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.

  4. # 4 楼答案

    multi将保留对数组本身的引用,multi[0][1]将引用数组中的元素

  5. # 5 楼答案

    从阵列中访问每个项目并将其打印出来:

    for(int i=0;i<2;i++){
                for(int j=0;j<5;j++){
                    System.out.println(multi[i][j]);
                }
            }
    
  6. # 6 楼答案

    这是因为,不是打印存储在数组中的值,而是打印数组的状态。你就是这么说的 System.out.println(multi); 这意味着 System.out.println(multi.toString());