繁体   English   中英

拆分字符串数组使我的数组索引超出范围

[英]Splitting a string array gives me array index out of bounds

好的,我正在尝试使用此代码生成切片地图。 但是,我继续使数组索引超出范围。 因此,这是如何在文本文件中添加“路径”的。 它拥有不同的数字,每个数字代表自己的瓷砖纹理。 文本文件的前2个数字是我们使用的宽度和高度。 此for循环正在执行的操作是将tile [x] [y]的每个数组分配给其所属位置中的tile。 这是我正在使用的文本文件:

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 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]);
        }
    }

IndexOutOfBounds是由于(x+ y*(width))+2表达式引起的。 但是,如果您只是试图将每个tile的值保存在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 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) ,因此我在后面使用了。

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

暂无
暂无

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

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