gpt4 book ai didi

java - 将信息从文件移至 ArrayList

转载 作者:行者123 更新时间:2023-12-01 06:34:56 25 4
gpt4 key购买 nike

当我尝试将数据从文本文件移动到数组列表时,我在 data.add(row); 行上收到错误(没有找到合适的方法)我该如何解决?如果我想用 arraylist 来填充 java jtable(swing) ,使用 arraylist 是个好主意吗?

 public ArrayList<String> getinformationforthetable() {
Scanner s = null;
ArrayList<String> data = new ArrayList<String>();
try {
s = new Scanner(new File("info.txt"));
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("")) {
String[] atoms = line.split("[#]");
ArrayList<String> row = new ArrayList<String>();
row.add(atoms[0]);
row.add(atoms[1]);
row.add(atoms[2]);
row.add(atoms[3]);
row.add(atoms[4]);
row.add(atoms[5]);
data.add(row);

}
}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
if (s != null) {
s.close();
}
}
return data;
}
}

最佳答案

此错误是由于您的数据数组列表是字符串数组列表

  ArrayList<String> data = new ArrayList<String>();

并且您正在尝试向其中添加另一个数组列表。你应该将其更改为

  List<List<String>> data = new ArrayList<List<String>>();

希望这有帮助。

关于java - 将信息从文件移至 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22214244/

25 4 0
文章推荐: java - Java 中的内联数组定义
文章推荐: jquery - 删除容器