Android java读取文件将结果传递给方法

发布于 2024-11-27 04:12:29 字数 1636 浏览 2 评论 0原文

我认为我缺少 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2024-12-04 04:12:29

问题出在内部 while 循环。

The problem was the inner while loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文