我想我错过了java语言或一般编程的一些基本原则。我读了一个文本文件与整数成一个2d数组(这似乎工作得很好),并通过2d数组从另一个类的方法,但当打印结果从那里我得到所有的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,rn]+");
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];
}
}
}
}
问题是内部while循环