如何将数组列表的每一行分配给自己的数组

时间:2019-02-12 14:03:13

标签: java

我能够将csv文件转换为arraylist,但是我希望能够找到我的arraylist中每一行的均值和标准差。为此,我希望将每一行转换为一个单独的数组,以供将来使用。

    BufferedReader gradeBuffer = null;

    try {
        String gradeLine;
        gradeBuffer = new BufferedReader(new FileReader("Assignment4-datafile.csv"));

        // Read in file line by line
        while ((gradeLine = gradeBuffer.readLine()) != null) {
            System.out.println(gradeCSVtoArrayList(gradeLine));
        }
        //throw exception if file not found
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (gradeBuffer != null) gradeBuffer.close();
        } catch (IOException gradeException) {
            gradeException.printStackTrace();
        }
    }
}

// Convert CSV to ArrayList using Split
public static ArrayList<String> gradeCSVtoArrayList(String gradeCSV) {
    ArrayList<String> gradeResult = new ArrayList<String>();

    if (gradeCSV != null) {
        String[] splitData = gradeCSV.split("\\s*,\\s*");
        for (int i = 0; i < splitData.length; i++) {
            if (!(splitData[i] == null) || !(splitData[i].length() == 0)) {
                gradeResult.add(splitData[i].trim());

            }
        }
    }

    return gradeResult;

}

}

1 个答案:

答案 0 :(得分:0)

我认为您需要做的第一件事是更改gradeCSVtoArrayList以返回数字列表(长整型还是双整型,具体取决于它是整数还是浮点数)。

public static ArrayList<Long> gradeCSVtoArrayList(String gradeCSV) {
    ArrayList<Long> gradeResult = new ArrayList<Long>();

    if (gradeCSV != null) {
        String[] splitData = gradeCSV.split("\\s*,\\s*");
        for (int i = 0; i < splitData.length; i++) {
            if (!(splitData[i] == null) || !(splitData[i].length() == 0)) {
                gradeResult.add(Long.parseLong(splitData[i].trim()));

            }
        }
    }

    return gradeResult;
}

在此列表中,您可以找到均值和标准差。