第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Java中的csv文件中對行進(jìn)行分組

在Java中的csv文件中對行進(jìn)行分組

慕森王 2021-05-04 17:54:34
我是Java的新手,我必須解析一個.csv文件。該文件在每一行中包含學(xué)生的ID,他們通過的科目ID和他們通過的科目成績。例如:Student ID,Subject ID,Grade1,A1-102,71,A1-103,61,A1-104,51,A1-108,92,A1-101,52,A1-105,7我需要計算一個學(xué)生通過的類似于SQL's GROUP BYEg的課程的數(shù)量:SELECT count(*) FROM STUDENTS GROUP BY Student_ID;假設(shè)csv文件已打開并可以讀取,是否可以為一個學(xué)生對多個條目進(jìn)行分組?我的代碼:csvFile = "C:\\Myfile.csv";             try {            br = new BufferedReader(new FileReader(csvFile));            while ((line = br.readLine()) != null) {              // what do i need to do here?            }        } catch (FileNotFoundException e) {            System.out.println("File not found\n");        } catch (IOException e) {            System.out.println("An I/O exception has occured\n");        } finally {                if (br != null)                try {                    br.close();                } catch (IOException e) {                    System.out.println("File is already closed");                }            }有什么想法嗎?
查看完整描述

3 回答

?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗 獲得超9個贊

對于數(shù)據(jù)組織而言,擁有一個arraylist并不是最佳的解決方案。我附加了最后一個解決方案,以引入一個哈希表,該哈希表存儲由學(xué)生ID標(biāo)識的數(shù)組列表。有些事情是一樣的,例如for循環(huán)需要確切的學(xué)生人數(shù)。


BufferedReader br = null;

    //this is the master HashMap, a datastructure which points to n amount of separate arraylist objects.

    HashMap<String, ArrayList<String>> master = new HashMap<>();


    //x = 3 for demonstration purposes replace the value with the 

    //actual number of students 

    for(int x = 1; x <= 3; x++) {


        try {

            String line;

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


            br = new BufferedReader(new FileReader(csvFile.toString()));

            String StudentIDNeeded = Integer.toString(x);


            while ((line = br.readLine()) != null) {


                if (line.substring(0, 1).equals(StudentIDNeeded)) {

                    result.add(line.substring(2).toString());

                }

            }


            master.put(Integer.toString(x),result);


        } catch (FileNotFoundException e) {

            System.out.println("File not found\n");

        } catch (IOException e) {

            System.out.println("An I/O exception has occured\n");

        } finally {

            if (br != null)

                try {

                    br.close();

                } catch (IOException e) {

                    System.out.println("File is already closed");

                }

        }


    }


    System.out.println("Hash Size:"+master.size());

    System.out.println("Hash Contents" + master.toString());

}

此代碼塊輸出這兩個字符串


Hash Size:3

Hash Contents{1=[A1-102,7, A1-103,6, A1-104,5, A1-108,9], 2=[A1-101,5], 

3=[A1-105,7, A1-101,5]}

該解決方案應(yīng)該通過利用哈希圖中的許多數(shù)組列表來擴(kuò)展到更大的數(shù)據(jù)集。


查看完整回答
反對 回復(fù) 2021-05-19
  • 3 回答
  • 0 關(guān)注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號