Android java读取文件将结果传递给方法
我认为我缺少 java 语言或一般编程的一些基本原理。 我已将带有整数的文本文件读取到二维数组中(似乎工作得很好),并将二维数组传递到另一个类的方法中,但是当从那里打印结果时,我得到全 0。我有一种预感,这是我忽略或没有意识到的基本而简单的事情。
这是我在类 Maps 中填充代码的位置:
public void load() {
mapWidth = 40;
mapHeight = 32;
//int[][] baseLayer = new int[mapWidth][mapHeight];
baseLayer = new int[mapWidth][mapHeight];
try {
is = file.readAsset("maps/townMap.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("android", "could not load file");
}
scanner = new Scanner(is);
scanner.useDelimiter("[\\s,\r\n]+");
try {
for (int i = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
baseLayer[i][j] = scanner.nextInt();
}
}
}
}
} finally {
scanner.close();
}
testMap = new AndroidMap(mapWidth, mapHeight, baseLayer, tMapLayer2);
currentMap = testMap;
}
这是我尝试复制类 AndroidMap 中的内容的位置: 公共类 AndroidMap {
int mapWidth;
int mapHeight;
int[][][] map;
public AndroidMap(int mapWidth, int mapHeight, int[][] baseLayer, int[][]midLayer) {
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
map = new int[mapWidth][mapHeight][2];
for (int i = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
map[i][j][0] = baseLayer[i][j];
map[i][j][1] = midLayer[i][j];
}
}
}
}
I think I am missing some basic principles of the java language, or programming in general.
I have read a text file with integers into a 2d array (which seems to work just fine) and pass the 2d array into a method from another class but when printing the results from there I get all 0s. I have a hunch this is something basic and simple that I have overlooked or am unaware of.
Here is where I populate the code in class Maps:
public void load() {
mapWidth = 40;
mapHeight = 32;
//int[][] baseLayer = new int[mapWidth][mapHeight];
baseLayer = new int[mapWidth][mapHeight];
try {
is = file.readAsset("maps/townMap.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("android", "could not load file");
}
scanner = new Scanner(is);
scanner.useDelimiter("[\\s,\r\n]+");
try {
for (int i = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
baseLayer[i][j] = scanner.nextInt();
}
}
}
}
} finally {
scanner.close();
}
testMap = new AndroidMap(mapWidth, mapHeight, baseLayer, tMapLayer2);
currentMap = testMap;
}
Here is where I try copying the contents in class AndroidMap:
public class AndroidMap {
int mapWidth;
int mapHeight;
int[][][] map;
public AndroidMap(int mapWidth, int mapHeight, int[][] baseLayer, int[][]midLayer) {
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
map = new int[mapWidth][mapHeight][2];
for (int i = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
map[i][j][0] = baseLayer[i][j];
map[i][j][1] = midLayer[i][j];
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在内部 while 循环。
The problem was the inner while loop.