简体   繁体   中英

What is wrong with this 2D Array?

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

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.

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 ie

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.

将第一行更改为board[][] ={{ all your numbers}} ,一切正常。

您的声明是错误的。

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

从声明中删除尺寸:

int board[][]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM