[英]Splitting a string array gives me array index out of bounds
Okay, I am trying to generate a tile map with this code. 好的,我正在尝试使用此代码生成切片地图。 However, I keep on getting an array index out of bounds.
但是,我继续使数组索引超出范围。 So, how this works is that for the "path" I add in a text file.
因此,这是如何在文本文件中添加“路径”的。 It holds different numbers each representing its own tile texture.
它拥有不同的数字,每个数字代表自己的瓷砖纹理。 The first 2 numbers of the text file is the width and height of it in which we use.
文本文件的前2个数字是我们使用的宽度和高度。 What this for loop is doing is assigning each array of tiles[x][y] to a tile in a position where it belongs.
此for循环正在执行的操作是将tile [x] [y]的每个数组分配给其所属位置中的tile。 Here is the text file I am using:
这是我正在使用的文本文件:
15 5 15 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 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
(there is not extra spaces between the lines idk why it did that) (idk行之间没有多余的空格,为什么这样做)
if there is anything i need to clear up let me know 如果有什么需要清理的,请告诉我
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]);
}
}
The IndexOutOfBounds
is due to (x+ y*(width))+2
expression. IndexOutOfBounds
是由于(x+ y*(width))+2
表达式引起的。 But if you are just trying to hold each tile's value in tile[][]
, there is a simpler way in which it can be done!! 但是,如果您只是试图将每个tile的值保存在
tile[][]
,则有一种更简单的方法可以完成它!
I'm assuming that your loadTextAsString(path)
is somewhat like this: 我假设您的
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();
}
This returns the textual representation of your file like shown in below example: 这将返回文件的文本表示,如以下示例所示:
15 5
15 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 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 22 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 33 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 44 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 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Now, let's start with actual method that will put all these values in a 2-D array. 现在,让我们从将所有这些值放入二维数组的实际方法开始。
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;
}
I hope this is what you were trying to achieve. 我希望这是您想要实现的目标。
Note: I hope your TextUtility.parseToInt(String val)
method is equivalent to Integer.parseInt(String val)
, hence I've used the later. 注意:我希望您的
TextUtility.parseToInt(String val)
方法等效于Integer.parseInt(String val)
,因此我在后面使用了。
您输入的高度为5,但只有4行图块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.