1

我有点卡住了。我该如何让它工作或有更好的方法?请给出代码示例。

public char[][] charmap = new char[SomeInts.amount][SomeInts.amount];
public void loadMap() throws IOException{
    BufferedReader in = new BufferedReader(new FileReader("map1.txt"));
    String line = in.readLine();
    while (line != null){
        int y = 0;
        for (int x = 0; x < line.length(); x++){

            //Error
            charmap[x][y] = line[x];
            //
        }
        y++;
    }
}
4

3 回答 3

4

该语法line[x]是为数组保留的。字符串不是数组。您可以使用该String#charAt方法并编写:

charmap[x][y] = line.charAt(x);
于 2012-10-02T08:59:14.437 回答
1

使用String.charAt(int)从字符串中获取字符..

于 2012-10-02T08:59:51.503 回答
1

试试这个。

char[][] mapdata = new char[SomeInts.amount][SomeInts.amount];

public void loadMap() throws IOException{
    BufferedReader in = new BufferedReader(new FileReader("map1.txt"));
    String line = in.readLine();
    ArrayList<String> lines = new ArrayList<String>();
    // Load all the lines
    while (line != null){
        lines.add(line);
    }
    // Parse the data
    for (int i = 0; i < lines.size(); i++) {
        for (int j = 0; j < lines.get(i).length(); j++) {
            mapdata[j][i] = lines.get(i).charAt(j);
        }
    }
}

希望这可以帮助。

于 2012-10-02T09:07:43.200 回答