zoukankan      html  css  js  c++  java
  • AJPFX关于TreeSet集合的介绍

    需求:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台。分析:1、创建键盘录入对象;
              2、创建TreeSet集合,使用匿名内部类实现Comparator接口,重写compara方法
              3、判断集合中元素的个数,向其中添加元素
              4、遍历集合

    class Demo_TreeSet{
             public static void main(String[] args){
                    Scanner sc = new Scanner(System.in);
                    System.out.println("请输入学生成绩格式,语文成绩,数学成绩,英语成绩,总成绩");
                    TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>{
                           public int compara( Student s1,Student s2);
                                      int num = s2.getSum() - s1.getSum();
                                      return num == 0 ? 1 : num;
                    });
                   while(ts.size()<5){
                        String line = sc.nextLine();                                       
                        String[] arr = line.Split(",");
                        int chinese = Integer.paserInt(arr[1]);
                        int math = Integer.paserInt(arr[2]);
                        int  english = Integer.paserInt(arr[3]);
                        ts.add(new Student(arr[0],chinese,math,english));
                    }
                    for(Student : s : ts){
                          System.out.println(s);
                     }
              }
    }
    class Student{
        private String name;
        private int chinses;
        private int math;
        private int enlish;

        public Student() {}



        public Student(String name, int chinese, int math, int english) {
                    super();
                    this.name = name;
                    this.chinese = chinese;
                    this.math = math;
                    this.english = english;
                    this.sum = this.chinese + this.math + this.english;
            }
            public int getSum() {
                    return sum;
            }
            
            public String toString() {
                    return name + "," + chinese + "," + math + "," + english + "," + sum;
            }
    }

  • 相关阅读:
    mysql之触发器before和after的区别
    字段与属性的区别
    功能性和非功能性需求 UP中FURPS+模型需求分类方式
    脏读、不可重复读 共享锁、悲观锁 和 事务五种隔离级别
    抽象类、接口的区别 和 抽象类可以不实现接口的全部方法
    错误码:2003 不能连接到 MySQL 服务器在 (10061)
    在ubuntu下使用mysql API读取数据库的乱码问题
    vs2010下htmlcxx的编译以及环境的搭建
    effective c++ 条款15 在资源管理类中提供对原始资源的访问
    Qt 依赖包的加载
  • 原文地址:https://www.cnblogs.com/AJPFX/p/10852162.html
Copyright © 2011-2022 走看看