当前位置:网站首页>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]边栏推荐
- Oracle彻底卸载的完整步骤
- Is it safe to open a stock account on the compass?
- ZABBIX distributed system monitoring
- 今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
- Coscon'22 lecturer solicitation order
- [observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology
- Handling of NPM I installation problems
- Kingbasees plug-in DBMS of Jincang database_ UTILITY
- Jincang KFS data centralized scenario (many to one) deployment
- OODA working method
猜你喜欢

Open source invites you to participate in the openssf Open Source Security Online Seminar

Compilation of learning from Wang Shuang (1)
![[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology](/img/82/8cac87231e51698ab17f1274b3a0bd.jpg)
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology

仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)

一个数学难题,难倒两位数学家

ES 学习
![[file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?](/img/28/ab02d38bde47053b155e0545b47039.png)
[file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?

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

Getting started with Apache Shenyu

How to start the phpstudy server
随机推荐
Performance file system
scrapy+scrapyd+gerapy 爬虫调度框架
手机办理长投学堂证券开户靠谱安全吗?
查询法,中断法实现USART通信
手机炒股安全吗?
Complete steps for a complete Oracle uninstall
持续交付-Jenkinsfile 语法
16 enterprise architecture strategies
Multiple environment variables
keep-alive
NuxtJS实战案例
XSS攻击
【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
每日3題(3)-檢查整數及其兩倍數是否存在
CDN+COS搭建图床超详细步骤
TASK03|概率论
Cdn+cos ultra detailed steps for drawing bed construction
一个五年北漂的技术er,根据这些年的真实经历,给应届生的一些建议
Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
Google Earth Engine(GEE)——evaluate实现一键批量下载研究区内的所有单张影像(上海市部分区域)