This site requires JavaScript, please enable it in your browser!
Greenfoot back
Raideron
Raideron wrote ...

2015/1/5

Error with 2d array

Raideron Raideron

2015/1/5

#
I'm having trouble with my code. I want to add a 2d array, but I get the error: ']' expected.
  int Coor[][] = new int[10][2];
    Coor[0][0] = 1;
    Coor[0][1] = 1;
    Coor[1][0] = 2;
    Coor[1][1] = 3;
    Coor[2][0] = 4;
    Coor[2][1] = 5;
    Coor[3][0] = 6;
    Coor[3][1] = 7;
    Coor[4][0] = 8;
    Coor[4][1] = 9;
    Coor[5][0] = 10;
    Coor[5][1] = 2;
    Coor[6][0] = 3;
    Coor[6][1] = 4;
    Coor[7][0] = 5;
    Coor[7][1] = 6;
    Coor[8][0] = 7;
    Coor[8][1] = 8;
    Coor[9][0] = 9;
    Coor[9][1] = 10;
I have no idea what's wrong. If anyone knows what's wrong please help. Thanks.
davmac davmac

2015/1/5

#
There's nothing wrong with the code you posted. Where exactly do you get the error?
Raideron Raideron

2015/1/5

#
The error is between the
Coor[
and
0][0] = 1;
danpost danpost

2015/1/5

#
This should not make a difference, but maybe through greenfoot it might. Try this for the first line:
int[][] Coor = new int[10][2];
Raideron Raideron

2015/1/5

#
No, still the same error.
danpost danpost

2015/1/5

#
Maybe show the entire method this code is in. If it is not in a method, then that is why you are getting the error. To define the values outside a method you should use this:
int Coor[][] = { { 1, 1 }, { 2, 3 }, { 4, 5 }, {6, 7 }, { 8, 9 }, { 10, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 } };
Raideron Raideron

2015/1/5

#
It was indeed not in a method, this fixed it, thank you!
You need to login to post a reply.