这个二维数组有什么问题?

发布于 2024-12-16 02:04:01 字数 1319 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

放血 2024-12-23 02:04:01

您不能在声明中指定数组的大小。如果删除大小整数,它将编译。但是,当您这样声明变量时,您应该始终将括号放在变量的类型上,而不是放在变量本身上:

int[][] board;

而不是:

int board[][];

如果您想创建具有特定大小的空白数组,那么您可以使用 new 运算符:

int[][] board = new int[21][21];

但是,如果通过数组初始化定义数组,则永远不会指定大小,即

int[][] board = {{...}, ...};

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:

int[][] board;

rather than:

int board[][];

If you want to create a blank array with a specific size, then you would do so with the new operator:

int[][] board = new int[21][21];

However, you never specify a size if you define your array though array initialization i.e.

int[][] board = {{...}, ...};
水水月牙 2024-12-23 02:04:01

删除 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]. Here int[] is a type (int array). It is similar to int * 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.

美煞众生 2024-12-23 02:04:01

首先,您会收到一个编译错误,用于在声明中告知 2D 数组的大小,

int board[21][21]

通常您希望这样做:

int board[][] = {{1,2}{1,3}}

This is a 2D array with 2 columns and 2 rows

 1 2
 1 3

因此,如果您想要一个具有 21 行的 2D 数组并且21 列你应该这样做:

int board[][] = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{],{ },{},{},{},{}}

并在内括号之间填写您需要的任何内容。

Firstly, you get a compile error for telling the size of your 2D array in the declaration

int board[21][21]

usually you want to do it this way:

int board[][] = {{1,2}{1,3}}

This is a 2D array with 2 columns and 2 rows

 1 2
 1 3

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.

如若梦似彩虹 2024-12-23 02:04:01

将第一行更改为 board[][] ={{ all yournumbers}} 一切都会好的。

Change first line to board[][] ={{ all your numbers}} and all will be ok.

风吹过旳痕迹 2024-12-23 02:04:01

你的声明是错误的。

 int board[][] = new int[][] {{...

Your declaration is wrong.

 int board[][] = new int[][] {{...
漫漫岁月 2024-12-23 02:04:01

从声明中删除尺寸:

int board[][]

remove dimensions from declaration:

int board[][]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文