abe*_*abe 8 java arrays arraylist
我有一个大学作业,要求我从 .csv 文件中获取数据,并以三种不同的方法读取、处理和打印它。这些说明要求我将数据读入数组列表中,我为此编写了一些代码,但我不确定我是否正确完成了操作。有人能帮我理解我应该如何将文件读入数组列表吗?
我的代码:
public void readData() throws IOException { 
    int count = 0;
    String file = "bank-Detail.txt";
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = "";
        while ((line = br.readLine()) != null) {
            bank.add(line.split(","));
            String[][] v = (String[][]) bank.toArray(new String[bank.size()][12]);
        }
    } catch (FileNotFoundException e) {
    }
}
你不需要2D数组来存储文件内容,一个 String[] 数组列表就可以了,例如:
public List<String[]> readData() throws IOException { 
    int count = 0;
    String file = "bank-Detail.txt";
    List<String[]> content = new ArrayList<>();
    try(BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line = "";
        while ((line = br.readLine()) != null) {
            content.add(line.split(","));
        }
    } catch (FileNotFoundException e) {
      //Some error logging
    }
    return content;
}
此外,在您的情况下,最好在list本地声明并返回它,method而不是将元素添加到共享list(“银行”)中。
| 归档时间: | 
 | 
| 查看次数: | 38600 次 | 
| 最近记录: |