当前位置:网站首页>Comparable的使用(用于Arrays.sort)
Comparable的使用(用于Arrays.sort)
2022-06-25 10:57:00 【用户9854323】
package snippet;
import java.util.Arrays;
/**
* 使用Comparable接口:让待排序对象所在的类实现Comparable接口,并重写Comparable接口中的compareTo()方法
* 缺点是只能按照一种规则排序
* @author tong
*
*/
public class Compara {
public static void main(String[] args) {
Person[] persons = new Person[5];
persons[0] =new Person("tom",45);
persons[1] =new Person("jack",12);
persons[2] =new Person("bill",21);
persons[3] =new Person("kandy",34);
persons[4] =new Person();
//关键
Arrays.sort(persons);
for (Person person:persons) {
System.out.println(person);
}
}
}下面是继承Comparable的Person类:
package snippet;
class Person implements Comparable<Person>{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public Person(){
this("unknown", 0);
}
//重写Student类的toString()方法,在输入对象时按照以下方式输出
@Override
public String toString() {
return "Person[name:"+name+",age:"+age+"]";
}
//重写该类的compareTo()方法,使其按照从小到大顺序排序
@Override
public int compareTo(Person o) {
return age-o.age;
}
}输出结果:
Person[name:unknown,age:0]
Person[name:jack,age:12]
Person[name:bill,age:21]
Person[name:kandy,age:34]
Person[name:tom,age:45]边栏推荐
- Detailed explanation of Android interview notes handler
- 【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
- Introduction to socket UDP and TCP
- Explanation and use of kotlin syntax for Android
- Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
- Handling of NPM I installation problems
- 每日3题(3)-检查整数及其两倍数是否存在
- Daily 3 questions (3) - check whether integers and their multiples exist
- 垃圾回收机制
- Google Earth Engine(GEE)——evaluate實現一鍵批量下載研究區內的所有單張影像(上海市部分區域)
猜你喜欢

中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析

一个五年北漂的技术er,根据这些年的真实经历,给应届生的一些建议

Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance

Compilation of learning from Wang Shuang (1)

Netease's open source distributed storage system curve officially became the CNCF sandbox project

Query method and interrupt method to realize USART communication

今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界

zabbix分布式系统监控

Sign up to open the third session of the "flying oar hacker marathon". It's been a long time

金仓KFS数据集中场景(多对一)部署
随机推荐
Ouverture de l'inscription | le troisième marathon des hackers de pagaie est arrivé comme prévu.
A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students
Output reading: apply what you have learned
Performance network
Five types of questions about network planning
Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
1-7snapshots and clones in VMWare
Coscon'22 lecturer solicitation order
Jincang KFS data centralized scenario (many to one) deployment
Advanced single chip microcomputer -- development of PCB (2)
GaussDB 集群维护案例集-sql执行慢
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
Software testing to avoid being dismissed during the probation period
一个数学难题,难倒两位数学家
无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》
撸一个随机数生成器
2022年PMP项目管理考试敏捷知识点(2)
Binder explanation of Android interview notes
Google Earth engine (GEE) - evaluate enables one click batch download of all single images in the research area (some areas in Shanghai)
Think about it