Java 常用类库 之 比较接口 Comparator
http://www.verejava.com/?id=169931036202101
/** 知识点: 比较类 Comparator 题目: 将某班学生按数学成绩从小到大排序 思路: 1. 抽象出类: 1.1 班级(ClassSet) 1.2 学生(Student) 2. 找出类关系: 2.1 学生 属于 班级 Student -> ClassSet(多对1) 3. 找出类属性: 3.1 ClassSet(班级名称,班级人数) 3.2 Student(学生名称,数学成绩) 4. 找出类方法: 4.1 学生添加到班级 ClassSet{addStudent(Student s)} 4.2 学生成绩从小到大排序 ClassSet{sortByScore()} */ import java.util.Arrays; import java.util.Comparator; public class TestComparator { public static void main(String[] args) { //实例化4G班级 ClassSet c = new ClassSet("4G", 4); //添加学生 c.addStudent(new Student("李明", 90)); c.addStudent(new Student("李浩", 80)); c.addStudent(new Student("王涛", 95)); c.addStudent(new Student("张胜", 70)); //获得4G班级学生数组集合 Student[] students = c.getStudents(); //输出学生信息 for (Student s : students) { if (s != null) System.out.println(s.getName() + "," + s.getMathScore()); } System.out.println("\n根据学生成绩升序排序"); Arrays.sort(students, new StudentAscComparator()); for (Student s : students) { if (s != null) System.out.println(s.getName() + "," + s.getMathScore()); } System.out.println("\n根据学生成绩降序排序"); Arrays.sort(students, new StudentDescComparator()); for (Student s : students) { if (s != null) System.out.println(s.getName() + "," + s.getMathScore()); } } } class ClassSet { private String className;//班级名称 private int maxSize;//班级学生人数 private int currentSize;//当前多少学生 private Student[] students;//所有学生的数组 public ClassSet(String className, int maxSize) { this.className = className; this.maxSize = maxSize; students = new Student[maxSize]; } public Student[] getStudents() { return this.students; } /** 添加学生 */ public void addStudent(Student s) { for (int i = 0; i < students.length; i++) { if (students[i] == null) { students[i] = s; currentSize++; break; } } } } class Student { private String name;//学生姓名 private int mathScore;//数学成绩 public Student(String name, int mathScore) { this.name = name; this.mathScore = mathScore; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getMathScore() { return this.mathScore; } public void setMathScore(int mathScore) { this.mathScore = mathScore; } } /** 学生升序排列 */ class StudentAscComparator implements Comparator { public int compare(Object o1, Object o2) { if ((o1 instanceof Student) && (o2 instanceof Student)) { Student s1 = (Student) o1; Student s2 = (Student) o2; if (s1.getMathScore() > s2.getMathScore()) return 1; if (s1.getMathScore() < s2.getMathScore()) return -1; } return 0; } } /** 学生降序排列 */ class StudentDescComparator implements Comparator { public int compare(Object o1, Object o2) { if ((o1 instanceof Student) && (o2 instanceof Student)) { Student s1 = (Student) o1; Student s2 = (Student) o2; if (s1.getMathScore() > s2.getMathScore()) return -1; if (s1.getMathScore() < s2.getMathScore()) return 1; } return 0; } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java 常用类库 之 比较类 Comparable
http://www.verejava.com/?id=169930999133100 /** 知识点: 比较类 Comparable 题目: 将某班学生按数学成绩从小到大排序 思路: 1. 抽象出类: 1.1 班级(ClassSet) 1.2 学生(Student) 2. 找出类关系: 2.1 学生 属于 班级 Student -> ClassSet(多对1) 3. 找出类属性: 3.1 ClassSet(班级名称,班级人数) 3.2 Student(学生名称,数学成绩) 4. 找出类方法: 4.1 学生添加到班级 ClassSet{addStudent(Student s)} 4.2 学生成绩从小到大排序 ClassSet{sortByScore()} */ import java.util.Arrays; public class TestComparable { public static void main(String[] args) { //实例化4G班级 ClassSet c = new ClassSet("4G", 4); //添加学生 c.addStudent...
- 下一篇
Java 11 正式发布,这 8 个逆天新特性教你写出更牛逼的代码
美国时间 09 月 25 日,Oralce 正式发布了 Java 11,这是据 Java 8 以后支持的首个长期版本。 为什么说是长期版本,看下面的官方发布的支持路线图表。 可以看出 Java 8 扩展支持到 2025 年,而 Java 11 扩展支持到 2026 年。 现在大部分都在用 Java 8,Java 9 和 10 目前很少有人在用,至少我没有发现有公司在生产环境应用的,那就是找死。 现在 Java 11 长期支持,也已经包含了 9 和 10 的全部功能,9 和 10 自然就活到头了。。 那么我们来看下 从 Java 9 - 11 都有哪些重要的新特性呢? 1、本地变量类型推断 这个博主已经写过一篇文章,详细的介绍了 Java 10 带来的这个新特性。 什么是局部变量类型推断? varjavastack="javastack"; System.out.println(javastack); 大家看出来了,局部变量类型推断就是左边的类型直接使用var定义,而不用写具体的类型,编译器能根据右边的表达式自动推断类型,如上面的String。 varjavastack="javasta...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- CentOS6,CentOS7官方镜像安装Oracle11G
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题