这个二维数组有什么问题?
我在 Java 中使用二维数组,这是第一次这样做。我想知道数组出了什么问题,因为我收到了很多错误......? (可能是一个非常愚蠢的错误)
代码
int board[21][21] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,1},
{1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1},
{1,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
I'm using a 2D Array in Java and it's the first time doing so. I'm wondering what is wrong with the array as I'm getting many errors...? (probably a really stupid error)
Code
int board[21][21] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,1},
{1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1},
{1,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
Error List
Here is a link instead of posting it here since it's a lot of errors
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不能在声明中指定数组的大小。如果删除大小整数,它将编译。但是,当您这样声明变量时,您应该始终将括号放在变量的类型上,而不是放在变量本身上:
而不是:
如果您想创建具有特定大小的空白数组,那么您可以使用
new
运算符:但是,如果通过数组初始化定义数组,则永远不会指定大小,即
You can't specify the size of arrays in the declaration. If you remove your size integers, it will compile. However, you should always place your brackets on your variable's type, not on the variable itself when you declare it as such:
rather than:
If you want to create a blank array with a specific size, then you would do so with the
new
operator:However, you never specify a size if you define your array though array initialization i.e.
删除 21. 您应该说
int board[][] = ....
您必须在创建数组时定义数组的维度。例如,您可以说
new int[5]
并创建 5 个元素的 long int 数组。但是,当您将此数组分配给变量时,您会说:int[] arr = new int[5]
。这里int[]
是一个类型(int 数组)。它类似于 C 中的int *
。在定义数组类型时,您不必指定数组有多大。您只需说出它的元素类型是什么以及它有多少个维度。Remove 21. You should say
int board[][] = ....
You have to define dimensions of array when creating them. For example you can say
new int[5]
and create 5 elements long int array. But when you assign this array to variable you say:int[] arr = new int[5]
. Hereint[]
is a type (int array). It is similar toint *
in C. You do not have to say how big will be the array when you are defining its type. You just have to say what is its element type and how many dimensions it will have.首先,您会收到一个编译错误,用于在声明中告知 2D 数组的大小,
通常您希望这样做:
This is a 2D array with 2 columns and 2 rows
因此,如果您想要一个具有 21 行的 2D 数组并且21 列你应该这样做:
int board[][] = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{],{ },{},{},{},{}}
并在内括号之间填写您需要的任何内容。
Firstly, you get a compile error for telling the size of your 2D array in the declaration
usually you want to do it this way:
This is a 2D array with 2 columns and 2 rows
So if you want to a 2D array with 21 rows and 21 columns you should do this:
int board[][] = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{],{},{},{},{},{}}
and fill whatever you need in between the inner brackets.
将第一行更改为
board[][] ={{ all yournumbers}}
一切都会好的。Change first line to
board[][] ={{ all your numbers}}
and all will be ok.你的声明是错误的。
Your declaration is wrong.
从声明中删除尺寸:
remove dimensions from declaration: