gpt4 book ai didi

java - 是否可以声明 String[][] 类型的全局数组?

转载 作者:行者123 更新时间:2023-12-01 13:41:19 24 4
gpt4 key购买 nike

我想做的是:

class test{
public static String[][] p;

}

但是有些事情不对劲。当我尝试使用以下方法写入内容时:

p[][]={...};

它说:“p 无法解析为类型”。

编辑:好的,我发现理解我在说什么时存在一些问题,所以我在这里发布了一段代码。

public class Test{
static String[][] plansza;
public static void main(String []arg){

p[][]={ {" ","A", "B", "C", "D", "E", "F", "G", "H"},
{"1.","0","1","0","1","0","1","0","1"},
{"2.","1","0","1","0","1","0","1","0"},
{"3.","0","1","0","1","0","1","0","1"},
{"4.","0","0","0","0","0","0","0","0"},
{"5.","0","0","0","0","0","0","0","0"},
{"6.","2","0","2","0","2","0","2","0"},
{"7.","0","2","0","2","0","2","0","2"},
{"8.","2","0","2","0","2","0","2","0"}
};
}

最佳答案

要正常创建数组,您至少需要设置数组的第一个维度,但通常您会这样做

1) 新类型[dim1][dim2]..[dimN]

如果使用诸如

之类的元素初始化数组,则可以跳过维度

2) 新类型[][]..[]{{..{元素},},..}}

但是,如果您在声明数组的同一位置使用元素初始化数组,那么您可以跳过new Type[][].. .[] 部分,只需使用 {{..{elements},},..}} 就像

3) 类型[][]..[] myArray = {{..{elements},},..}}

<小时/>

在您的情况下,您遇到的是情况 2),因为您没有在声明数组的地方初始化数组。所以而不是

p[][] = {
{" ","A","B","C","D","E","F","G","H"},
...
};

你需要将其写为

p = new String[][]{
{" ","A","B","C","D","E","F","G","H"},
...
};

关于java - 是否可以声明 String[][] 类型的全局数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com