【问题标题】:Splitting a string array gives me array index out of bounds拆分字符串数组使我的数组索引超出范围
【发布时间】:2017-01-05 02:05:34
【问题描述】:

好的,我正在尝试使用此代码生成平铺地图。但是,我继续让数组索引越界。所以,它的工作原理是我在文本文件中添加的“路径”。它拥有不同的数字,每个数字代表自己的瓷砖纹理。文本文件的前 2 个数字是我们使用的它的宽度和高度。这个 for 循环所做的是将每个 tiles[x][y] 数组分配给它所属位置的瓦片。这是我正在使用的文本文件:

15 5

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2 2

2 2 2 2 2 2 2 2 2 2 2 2 2 2

2 2 2 2 2 2 2 2 2 2 2 2 2 2

(行之间没有多余的空格,idk 为什么这样做)

如果有什么我需要澄清的,请告诉我

 String textFile = TextUtility.loadTextAsString(path);


    String[] tileValue = textFile.split("\\s+");

    width = TextUtility.parseToInt(tileValue[0]);
    height = TextUtility.parseToInt(tileValue[1]);

     System.out.println(width+" "+height + " " + tileValue.length);
    tiles = new int[width][height];


    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {

            tiles[x][y] = TextUtility.parseToInt(tileValue[(x+y*(width))+2]);
            System.out.print(""+ tileValue[(x+ y*(width))+2]);
        }
    }

【问题讨论】:

  • 您是否进行了调试以验证您的代码?当你在做的时候,tileValue 可能是空的:tileValue[(x+y*(width))+2]
  • 要解决这个问题,您可以使用 if 语句检查 this-x+ y*(width))+2 是否小于数组的长度。
  • 输入的第一行是15 5,但我只能看到这之后的四行而不是五行!此外,tileValue[(x+y*(width))+2] 将超出范围。
  • 我现在使用了不同的值。我做了(在 for 循环之前) int i = 0;和 tileValue[i+2];
  • 现在它至少不会渲染我的图块了....

标签: java arrays tile


【解决方案1】:

IndexOutOfBounds 是由 (x+ y*(width))+2 表达式引起的。但是如果你只是想在tile[][] 中保存每个图块的值,有一种更简单的方法可以做到!

我假设您的loadTextAsString(path) 有点像这样:

public static String loadTextAsString(String path) {
        StringBuilder builder = new StringBuilder();
        try (BufferedReader fileReader = Files.newBufferedReader(Paths.get(path))) {
            String eachLine = "";
            while ((eachLine = fileReader.readLine()) != null) {
                builder.append(eachLine);
                builder.append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }

这将返回文件的文本表示,如下例所示:

15 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

现在,让我们从将所有这些值放入二维数组的实际方法开始。

public int[][] createTiles(String path){
        String textFile = loadTextAsString(path);

        //Get all individual lines in an array
        String[] allLinesInFile = textFile.split("\\n|\\r");

        int width = Integer.parseInt(allLinesInFile[0].split("\\s")[0]);
        int height = Integer.parseInt(allLinesInFile[0].split("\\s")[1]);

        System.out.println("Width -> " + width);
        System.out.println("Height -> " + height);

        //2-D array to hold the tiles
        int[][] tiles = new int[height][width];

        //Row count for the array
        int row = 0;
        for(String eachLine : allLinesInFile){
            String[] allTiles = eachLine.split("\\s");
        /*
         * This will ignore the very first line of the file with width and 
         * height and new line characters
         *
         */
            if(allTiles.length != width){
                continue;
            }

            //Column count for the array
            int col = 0;
            for(String eachTile : allTiles){

                tiles[row][col] = Integer.parseInt(eachTile);
               // Increment column
                col++;
            }
            // Increment Row
            row++;
        }
        //Return the 2-D array.
        return tiles;
    }

我希望这就是你想要达到的目标。

注意:我希望你的TextUtility.parseToInt(String val)方法和Integer.parseInt(String val)是等价的,所以我使用了后者。

【讨论】:

    【解决方案2】:

    您输入的高度为 5,但您只有 4 行图块。

    【讨论】:

    • 这很适合作为评论而不是答案。请在发布答案之前查看指南。
    • 对我来说似乎是这个问题的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多