简体   繁体   English

从csv文件中提取数据并放入2D数组 - 重构

[英]extract data from csv file and put to 2D Array - refactoring

I need read data from csv file and much more convinience for me is put there to 2D array (to my mind it's easiest way to work with this "schedule" data). 我需要从csv文件中读取数据,对我来说更方便的是放在2D数组中(我认为这是处理这个“计划”数据的最简单方法)。 Each file line contained information in following format: 每个文件行包含以下格式的信息:

Instructor, Course, Group, Student, Result

as follows example: 如下例子:

Paul Schwartz,Introduction to Computer Architecture,I1,Ben Dunkin,88
Muhamed Olji,Object Oriented Programming,I4,Mike Brown,73

But my code needs some simplify. 但我的代码需要一些简化。 But I don't know how to make it easier and ask of You . 但我不知道如何让它更容易,并问

Code: 码:

private String[][] fileContent(String pathToCSVFile) {
    final int ROWS = 100;
    final int COLUMNS = 5;
    String fileData[][] = new String[ROWS][COLUMNS];
    Scanner scanner = new Scanner(pathToCSVFile);
    boolean done = false;
    int i, j;

    while (!done) {
        for (i = 0; i >= 0; i++) {
           for (j = 0; j >= 0; j++) {
               String str[] = scanner.nextLine().split(","); 
               for (int element = 0; element < str.length; element++) {
                   fileData[i][element] = str[element];
                   if (i >= ROWS) {
                       Arrays.copyOf(fileData, fileData.length * 2);
                   }
               }                   
           }
        }

        if (!scanner.hasNextLine()) done = true;
    }

    return  fileData;
}
  • How to refactor this snippet of code for better simplicity? 如何重构这段代码以简化操作?
  • Does exist any better way for partially filled array (than Arrays.copyOf(fileData, fileData.length * 2) )? 是否存在部分填充数组的更好方法(比Arrays.copyOf(fileData, fileData.length * 2) )?

Using openCSV, you can get a list containing all the lines and convert it to an array (or just keep the list): 使用openCSV,您可以获得包含所有行的列表并将其转换为数组(或只保留列表):

try (CSVReader reader = new CSVReader(new BufferedReader(
          new FileReader(pathToCSVFile)));) {

    List<String[]> lines = reader.readAll();
    return lines.toArray(new String[lines.size()][]);
}

(using Java 7 try-with-resources syntax) (使用Java 7 try-with-resources语法)

First of all, be careful with those for loops. 首先,要小心那些for循环。 They are "almost" undefined loops, because they start with i,j=0, and loop while >=0 (always, until they overflow into a negative number). 它们是“几乎”未定义的循环,因为它们以i开始,j = 0,并且在> = 0时循环(始终,直到它们溢出为负数)。 And why do you need them anyway? 你为什么还需要它们呢? I think with you while and the for(element) you are done, right? 我和你在一起思考(元素)你做完了吧? Something like that (I didn't tried, is just to explain the concept) 这样的事情(我没试过,只是为了解释这个概念)

private String[][] fileContent(String pathToCSVFile) {
    final int ROWS = 100;
    final int COLUMNS = 5;
    String fileData[][] = new String[ROWS][COLUMNS];
    Scanner scanner = new Scanner(pathToCSVFile);
    boolean done = false;
    int i=0;

    while (!done) {
        String str[] = scanner.nextLine().split(","); 
        for (int element = 0; element < str.length; element++) {
            fileData[i][element] = str[element];
            if (i >= ROWS) {
                Arrays.copyOf(fileData, fileData.length * 2);
            }
        }
        if (!scanner.hasNextLine())
            done = true;
        else
            i++;
    }
    return  fileData;
}

By the way, why don't you use objects, like an ArrayList? 顺便问一下,为什么不使用像ArrayList这样的对象? It would make your life easier, so you don't have to worry about memory handling. 它会让你的生活更轻松,所以你不必担心内存处理。 You just add new objects. 您只需添加新对象。

Something like an ArrayList <ArrayList <String>> 类似于ArrayList <ArrayList <String>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM