日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

集合到文件数据排序改进版

發布時間:2024/4/13 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合到文件数据排序改进版 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

案例需求

  • 鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績)。要求按照成績總分從高到低寫入文本文件

  • 格式:姓名,語文成績,數學成績,英語成績 舉例:林青霞,98,99,100

分析步驟

  • 定義學生類

  • 創建TreeSet集合,通過比較器排序進行排序

  • 鍵盤錄入學生數據

  • 創建學生對象,把鍵盤錄入的數據對應賦值給學生對象的成員變量

  • 把學生對象添加到TreeSet集合

  • 創建字符緩沖輸出流對象

  • 遍歷集合,得到每一個學生對象

  • 把學生對象的數據拼接成指定格式的字符串

  • 調用字符緩沖輸出流對象的方法寫數據

  • 釋放資源

  • 代碼實現

    • 學生類

    public class Student {// 姓名private String name;// 語文成績private int chinese;// 數學成績private int math;// 英語成績private int english;public Student() {super();}public Student(String name, int chinese, int math, int english) {super();this.name = name;this.chinese = chinese;this.math = math;this.english = english;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getEnglish() {return english;}public void setEnglish(int english) {this.english = english;}public int getSum() {return this.chinese + this.math + this.english;} }

    測試類

    public class TreeSetToFileDemo {public static void main(String[] args) throws IOException {//創建TreeSet集合,通過比較器排序進行排序TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {//成績總分從高到低int num = s2.getSum() - s1.getSum();//次要條件int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;int num4 = num3 == 0 ? s1.getName().compareTo(s2.getName()) : num3;return num4;}});//鍵盤錄入學生數據for (int i = 0; i < 5; i++) {Scanner sc = new Scanner(System.in);System.out.println("請錄入第" + (i + 1) + "個學生信息:");System.out.println("姓名:");String name = sc.nextLine();System.out.println("語文成績:");int chinese = sc.nextInt();System.out.println("數學成績:");int math = sc.nextInt();System.out.println("英語成績:");int english = sc.nextInt();//創建學生對象,把鍵盤錄入的數據對應賦值給學生對象的成員變量Student s = new Student();s.setName(name);s.setChinese(chinese);s.setMath(math);s.setEnglish(english);//把學生對象添加到TreeSet集合ts.add(s);}//創建字符緩沖輸出流對象BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\ts.txt"));//遍歷集合,得到每一個學生對象for (Student s : ts) {//把學生對象的數據拼接成指定格式的字符串//格式:姓名,語文成績,數學成績,英語成績StringBuilder sb = new StringBuilder();sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());// 調用字符緩沖輸出流對象的方法寫數據bw.write(sb.toString());bw.newLine();bw.flush();}//釋放資源bw.close();} }

    ?

    超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

    總結

    以上是生活随笔為你收集整理的集合到文件数据排序改进版的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。