1. 程式人生 > >Java中List集合排序的方法 比較器的使用 根據學生對象數序 語文 英語成績總和進行sort排序

Java中List集合排序的方法 比較器的使用 根據學生對象數序 語文 英語成績總和進行sort排序

private system.in set swift ringbuf 直觀 turn @override encoding

package com.swift;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Test2_ObjectIO {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        /*
         * 第2題: 有五個學生,每個學生有3門課(語文、數學、英語)的成績,寫一個程序接收從鍵盤輸入學生的信息,
         * 輸入格式為:name,30,30,30(姓名,三門課成績),然後把輸入的學生信息按總分從高到低的順序寫入到一個名稱"stu.txt"文件中。
         * 要求:stu.txt文件的格式要比較直觀,打開這個文件,就可以很清楚的看到學生的信息。
         
*/ List<Student> list = new ArrayList<Student>(); Scanner scan = new Scanner(System.in); for (int i = 1; i <=5; i++) { System.out.println("請輸入第"+i+"學生信息:格式為 name 60 60 60"); String name = scan.next(); Float yuwen = scan.nextFloat(); Float shuxue
= scan.nextFloat(); Float yingyu = scan.nextFloat(); Student stu = new Student(name, yuwen, shuxue, yingyu); list.add(stu); } for(Student stu:list) { System.out.println(stu.toString()); } Collections.sort(list,
new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { float num=(o2.getShuxue()+o2.getYingyu()+o2.getYuwen())-(o1.getShuxue()+o1.getYingyu()+o1.getYuwen()); int i=(int) num; return i; } }); for(Student stu:list) { System.out.println(stu.toString()); } } } class Student { private String name; private float yuwen; private float shuxue; private float yingyu; public String getName() { return name; } public void setName(String name) { this.name = name; } public float getYuwen() { return yuwen; } public void setYuwen(float yuwen) { this.yuwen = yuwen; } public float getShuxue() { return shuxue; } public void setShuxue(float shuxue) { this.shuxue = shuxue; } public float getYingyu() { return yingyu; } public void setYingyu(float yingyu) { this.yingyu = yingyu; } public Student(String name, float yuwen, float shuxue, float yingyu) { super(); this.name = name; this.yuwen = yuwen; this.shuxue = shuxue; this.yingyu = yingyu; } public Student() { super(); } @Override public String toString() { return "Student [name=" + name + ", yuwen=" + yuwen + ", shuxue=" + shuxue + ", yingyu=" + yingyu + "]"; } }

比較器就是個匿名內部類,Collections.sort(list,Comparator<Student>(){});

然後實現其中的compare方法,返回一個int類型的值,正數 負數 0分別代表大於小於等於

增加IO流的內容代碼如下;

package com.swift;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Test2_ObjectIO {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        /*
         * 第2題: 有五個學生,每個學生有3門課(語文、數學、英語)的成績,寫一個程序接收從鍵盤輸入學生的信息,
         * 輸入格式為:name,30,30,30(姓名,三門課成績),然後把輸入的學生信息按總分從高到低的順序寫入到一個名稱"stu.txt"文件中。
         * 要求:stu.txt文件的格式要比較直觀,打開這個文件,就可以很清楚的看到學生的信息。
         */
        List<Student> list = initiateList();
        
        for(Student stu:list) {
            System.out.println(stu.toString());
        }
        
        Collections.sort(list, new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                float num=(o2.getShuxue()+o2.getYingyu()+o2.getYuwen())-(o1.getShuxue()+o1.getYingyu()+o1.getYuwen());
                int i=(int) num;
                return i;
            }
        });
        
        initiate_IO(list);
        
        
    }

    private static void initiate_IO(List<Student> list) {
        BufferedWriter bw;
        StringBuffer sb=new StringBuffer();
        for(Student stu:list) {
            sb.append(stu.toString());
            sb.append("\r\n");
        }
        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src\\stu.txt"),"utf-8"));//需要標明路徑
            bw.write(sb.toString());
            bw.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        
    }

    private static List<Student> initiateList() {
        List<Student> list = new ArrayList<Student>();
        Scanner scan = new Scanner(System.in);
        for (int i = 1; i <=5; i++) {
            System.out.println("請輸入第"+i+"學生信息:格式為 name 60 60 60");
            String name = scan.next();
            Float yuwen = scan.nextFloat();
            Float shuxue = scan.nextFloat();
            Float yingyu = scan.nextFloat();
            Student stu = new Student(name, yuwen, shuxue, yingyu);
            list.add(stu);
        }
        return list;
    }
}

class Student {
    private String name;
    private float yuwen;
    private float shuxue;
    private float yingyu;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getYuwen() {
        return yuwen;
    }

    public void setYuwen(float yuwen) {
        this.yuwen = yuwen;
    }

    public float getShuxue() {
        return shuxue;
    }

    public void setShuxue(float shuxue) {
        this.shuxue = shuxue;
    }

    public float getYingyu() {
        return yingyu;
    }

    public void setYingyu(float yingyu) {
        this.yingyu = yingyu;
    }

    public Student(String name, float yuwen, float shuxue, float yingyu) {
        super();
        this.name = name;
        this.yuwen = yuwen;
        this.shuxue = shuxue;
        this.yingyu = yingyu;
    }

    public Student() {
        super();
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", yuwen=" + yuwen + ", shuxue=" + shuxue + ", yingyu=" + yingyu + "]";
    }

}

Java中List集合排序的方法 比較器的使用 根據學生對象數序 語文 英語成績總和進行sort排序