Java中List集合排序的方法 比较器的使用 根据学生对象数序 语文 英语成绩总和进行sort排序

2018-01-13 20:34:28来源:cnblogs.com作者:Advancing-Swift人点击

分享
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 + "]";    }}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台