将信息从文件移动到ArrayList

use*_*038 0 java swing arraylist

当我尝试将数据从textfile移动到arraylist时,我在行data.add(row)上得到错误; (找不到合适的方法)我该如何解决?如果我想用它来填充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;
    }
}
Run Code Online (Sandbox Code Playgroud)

San*_*eev 5

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

  ArrayList<String> data = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)

并且您正在尝试向其添加另一个数组列表.你应该把它改成

  List<List<String>> data = new ArrayList<List<String>>();
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.