简体   繁体   English

2D字符数组Java的“不是声明”问题

[英]“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. 我有一个地图的二维char数组,其中数组中的每个位置都指向地图中该位置的字符。 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 '; 由于某些原因,我的n = map [....],e = ...等无法正常工作,并返回错误消息“ not statement”和“;”。 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 [] ] 这个想法是: map[(down + 1)]给您一个char[] ,然后您像访问任何数组一样访问此char[] [因此您使用了两个[] ]

应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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