简体   繁体   中英

“Not a statement” issue with 2d character array java

I have a 2d char array of a map, where each position in the array refers to the character in that position in the map. I also have the user's current position. I have checked my map contains values and that the position is correct and that I am not trying to reach anything out of bounds in the map. For some reason my n = map[....], e =... etc isn't working and comes back with the error 'not a statement' and '; needed' etc. I cannot see why this would not work. Any ideas?

 public String look(int[] position, char[][] mapArray)
    {
    char[][] map = mapArray;
    char n;
    char e;
    char s;
    char w;
    int across;
    int down;
    across = position[0];
    down = position[1];
    System.out.println(across);
    System.out.println(down);
    n = map[(down + 1),across];
    e = map[down, (across + 1)];
    s = map[(down - 1), across];
    w = map[down, (across - 1)];
    //System.out.println("Across" + across);
    //System.out.println("Down" + down);
    //System.out.println("N" + n);
    //System.out.println("E" + e);
    //System.out.println("S" + s);
    //System.out.println("W" + w);
    return "hello";
    }

它应该是:

 n = map[(down + 1)][across];

To access a 2d array you need:

n = map[(down + 1)][across]

instead of

n = map[(down + 1),across]

[same goes for the other 2d array accesses]

The idea is: map[(down + 1)] gives you a char[] , and then you access this char[] like any array [and thus you use two [] ]

应该是:

n = map[(down + 1)][across];

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