gpt4 book ai didi

java - 将 .txt 文件读入二维数组列表

转载 作者:行者123 更新时间:2023-12-01 04:14:21 26 4
gpt4 key购买 nike

我正在 Android 中创建一个游戏,其中的关卡将存储在单独的 .txt 文件中。每个级别将是一个字符网格,代表 map 上的不同项目,但每个级别的大小都不同,因此我想创建一段强大的代码来读取文件,并将每个级别存储在 2d 中arraylist,无论其大小是多少。

我的第一次尝试:

private void loadLevel(String filename) {
mapWidth = 0;
mapHeight = 0;
BufferedReader br = null;
try {
String line = null;
br = new BufferedReader(new InputStreamReader(mContext.getAssets().open(filename)));
while ((line = br.readLine()) != null) {
mapArray.add(getLevelLine(line));
mapHeight++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

private ArrayList<Character> getLevelLine(String line) {
ArrayList<Character> levelLine = new ArrayList<Character>();
if (line == null) return levelLine;
char[] lineArray = line.toCharArray();
for (char mapPiece : lineArray) {
levelLine.add(mapPiece);
}
mapWidth = lineArray.length;
return levelLine;
}

这有点低效,因为在每一行上都会重新计算mapWidth,并且它不起作用,因为读取了文本文件的第一个水平行,并将其存储在arraylist上的第一个垂直列中,因此它复制了文本文件,但 x 和 y 坐标交换。

尝试2:

    private void loadLevel(String filename) {
mapWidth = 0;
mapHeight = 0;
BufferedReader br = null;
try {
String line = null;
br = new BufferedReader(new InputStreamReader(mContext.getAssets().open(filename)));
while ((line = br.readLine()) != null) {
mapArray.add(new ArrayList<Character>());
char lineArray[] = line.toCharArray();
for (char mapPiece : lineArray) {
mapArray.get(mapHeight).add(mapPiece);
}
mapHeight++;
mapWidth = lineArray.length;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

这以相同的方式计算mapWidth,所以看起来仍然有点低效。希望通过在数组列表中添加一个空条目,我可以循环遍历每个元素的第 i 个元素。第二次尝试也没有正确增加mapHeight,因为在最后一次迭代中,mapHeight将增加,然后while循环将不会再次执行,但由于某种原因,即使我在while循环后从mapHeight减去1,我出现索引越界错误。更重要的是,通过手动设置mapWidth和mapHeight,我的第二次尝试似乎仍然在将其存储到arraylist时交换x和y坐标。

我有什么明显遗漏的东西吗?看来应该有一种相对简单的方法来做到这一点,不需要预先读取文本文件,并避免使用普通的字符数组。

预先感谢您的帮助!

最佳答案

我实际上不明白为什么你是第一个例子,你已经切换了行和列。而且你没有说你是通过什么来衡量你的表现的。我的意思是对您来说哪个更重要 - 速度还是内存消耗?

无论如何,我编写了一个示例 input.txt 文件

a b c d e

f g h i g k

l m nop q r

st

你v

w

public class ReadTxtArray {
private final static String filePath = "/home/michael/input.txt";
public static void main(String[] args) {
try (Scanner s = new Scanner(new BufferedReader(new FileReader(filePath)))) {
List<List<Character>> rows = new ArrayList<List<Character>>();
while(s.hasNextLine()) {
rows.add(createRow(s.nextLine()));
}
System.out.println(rows);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static List<Character> createRow(String line) {
char[] c = line.toCharArray();
List<Character> chars = Arrays.asList(ArrayUtils.toObject(c));
return chars;
}

}

[[a, b, c, d, e], [f, g, h, i, g, k], [l, m, n, o, p, q, r, ], [s, t]、[u、v]、[w]]

这是输出。这是你想要的吗?

我正在使用 try 和资源 block ,以防您不熟悉 java 7 功能。遗憾的是,它仍然无法在 Android 上使用。真是耻辱……

关于java - 将 .txt 文件读入二维数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19589909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com