Java 拆分字符串数组会使数组索引超出范围
好的,我正试图用这段代码生成一个平铺贴图。然而,我不断地得到一个数组索引。所以,这是如何工作的,对于我在文本文件中添加的“路径”。它包含不同的数字,每个数字表示其自己的瓷砖纹理。文本文件的前两个数字是我们使用的宽度和高度。这个for循环所做的是将每个tile数组[x][y]分配给它所属位置的一个tile。以下是我正在使用的文本文件: 15 5 11 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为什么这样做) 如果有什么我需要澄清的,请告诉我Java 拆分字符串数组会使数组索引超出范围,java,arrays,tile,Java,Arrays,Tile,好的,我正试图用这段代码生成一个平铺贴图。然而,我不断地得到一个数组索引。所以,这是如何工作的,对于我在文本文件中添加的“路径”。它包含不同的数字,每个数字表示其自己的瓷砖纹理。文本文件的前两个数字是我们使用的宽度和高度。这个for循环所做的是将每个tile数组[x][y]分配给它所属位置的一个tile。以下是我正在使用的文本文件: 15 5 11 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
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]);
}
}
String textFile=TextUtility.loadTextAsString(路径);
字符串[]tileValue=textFile.split(\\s+);
宽度=TextUtility.parseToInt(tileValue[0]);
高度=TextUtility.parseToInt(tileValue[1]);
System.out.println(宽度+高度+波浪值长度);
瓷砖=新整型[宽度][高度];
对于(int y=0;y
您输入的高度是5,但您只有4行瓷砖 索引自动边界是由(x+y*(宽度))+2
表达式引起的。但是,如果您只是想在tile[][]
中保存每个tile的值,那么有一种更简单的方法可以实现
我假设您的加载文本字符串(路径)
有点像这样:
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.511
2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4
555555555555 现在,让我们从实际方法开始,该方法将把所有这些值放入一个二维数组中
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.parseToInt(String val)
,因此我使用了后者。是否调试以验证代码?tileValue可能在执行以下操作时为空:tileValue[(x+y*(width))+2]要修复此问题,您只需使用if
语句检查此-x+y*(width))+2
是否小于数组的长度。第一行输入显示15 5
,但我只能看到后面的四行,而不是五行!另外,tileValue[(x+y*(width))+2]
将超出边界。我现在使用了不同的值。我做了(在for循环之前)int I=0;和tileValue[i+2];现在,它不会呈现我的瓷砖,至少这是一种方式…这是很适合作为一个评论,而不是一个答案。在发布答案之前,请先阅读指南。在我看来,这是问题的答案。