Java-对List集合中添加、展示、排序的简单应用

题目:(1) 使用List来存储学生的信息(学号id,姓名name,成绩score),并录入5个学生的信息
(2) 列出学生所有信息
(3) 查询成绩大于80分学生信息。
(4)打印最高分学生的信息
(5)打印平均分

1、先创建学生类

public class Student {
    private Integer id;
    private String name;
    private Double score;
    public Student() {
    }
    public Student(Integer id, String name, Double score) {
        super();
        this.id = id;
        this.name = name;
        this.score = score;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getScore() {
        return score;
    }
    public void setScore(Double score) {
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", score=" + score
                + "]";
    }

创建测试类:

public class Exercise01 {
    public static void main(String[] args) {

        //声明List
        List<Student> list = new ArrayList<>();
        Student s = new Student(101, "一号", 90D);
        list.add(s);
        s = new Student(102, "二号", 95D);
        list.add(s);
        s = new Student(103, "三号", 80D);
        list.add(s);
        s = new Student(104, "四号", 75D);
        list.add(s);
        s = new Student(105, "五号", 60D);
        list.add(s);
        showStu(list);
        selectSc(list);
        getMaxs(list);
        getAvg(list);

    }
    /**
     * 显示学生信息
     * @param list
     */
    public static void showStu(List<Student> list){

        for(Student s:list){
            System.out.println(s);
        }
    }

    /**
     * 查询成绩大于80分学生信息。
     * @param list
     */
    public static void selectSc(List<Student> list){
        System.out.println("成绩大于80分的同学:");
        for(Student s:list){
            if(s.getScore()>80){
                System.out.println(s);
            }
        }
    }
    /**
     * 打印最高分学生的信息
     * @param list
     */
    public static void getMaxs(List<Student> list){
        Double s = -1d;
        Student stu=null;
        for(Student student:list){
            if(student.getScore()>s){
                s=student.getScore();
                stu = student;
            }
        }
        System.out.println("最高分的同学:\n"+stu);
    }
    /**
     * 平均分
     * @param list
     */
    public static void getAvg(List<Student> list){
        Double avg = 0.0d;
        Double sum=0.0d;
        for(Student s:list){
            sum+=s.getScore();
        }
        avg = sum/list.size();
        System.out.println("平均分为:"+avg);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值