对象数组排序
通过年龄比较
Comparable接口
- 此处运用到
comparable
接口,其内置方法为comparaTo
- 返回类型为整形
public interface Comparable<T> {
public int compareTo(T o);
}
代码
class Student implements Comparable<Student> { //实现了Comparable接口的Student类
public String name;
public int age;
public Student(String name, int age) { //构造方法
this.name = name;
this.age = age;
}
@Override
//重写Comparable接口内的comparaTo方法
public int compareTo(Student o) {
return this.age - o.age; //即调用方法的对象 - 参数对象
}
public static void main(String[] args) {
Student student1 = new Student("John Smith", 10);
Student student2 = new Student("Don Michael", 20);
if(student1.compareTo(student2) > 0){
//若student1 - student2 > 0,则打印:
System.out.println("John Smith > Don Michael");
} else if (student1.compareTo(student2) < 0) {
//若student1 - student2 < 0,则打印:
System.out.println("John Smith < Don Michael");
}else{
//若student1 - student2 = 0,则打印:
System.out.println("John Smith = Don Michael");
}
}
}
//运行结果为:John Smith < Don Michael
通过姓名比较
字符串大小比较
-
字符串的大小比较是指按照字典次序对单个字符或字符串进行比较大小的操作,一般都是以ASCII码值的大小作为字符比较的标准。
-
比如
abc
和ade
比较大小- 因为双方第一个字母相等,都是
a
,所以比较对象均向后移一位 - 由于
b
<d
,所以abc
<ade
- 因为双方第一个字母相等,都是
代码
class Student implements Comparable<Student> {
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student o) {
if(this.name.compareTo(o.name) > 0){ //即调用方法的对象 - 参数对象
return 1;
} else if (this.name.compareTo(o.name) < 0) {
return -1;
}else {
return 0;
}
}
public static void main(String[] args) {
Student student1 = new Student("John Smith", 10);
Student student2 = new Student("Don Michael", 20);
if(student1.compareTo(student2) > 0){
//若student1 - student2 > 0,则打印
System.out.println("John Smith > Don Michael");
} else if (student1.compareTo(student2) < 0) {
//若student1 - student2 < 0,则打印
System.out.println("John Smith < Don Michael");
}else{
//若student1 - student2 = 0,则打印
System.out.println("John Smith = Don Michael");
}
}
}
对比
- 对两个类的重写方法进行对比,可以发现问题:
Comparable
这个接口具有局限性,一但这个类写死了,后期就无法进行修改- 可以理解为,写死的就是一个默认的比较方式
- 由此,我们可以使用一个新的接口
Comparator
比较器——Comparator接口
Comparator
接口返回类型也为整形- 其内部有很多内置方法
public interface Comparator<T> { //三处 T 必须都得一样
int compare(T o1, T o2);
//还有很多其他方法...
}
年龄比较器
import java.util.Comparator;
class Student {
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.age - o2.age;
}
public static void main(String[] args) {
Student student1 = new Student("John Smith", 18);
Student student2 = new Student("Don Michael", 28);
AgeComparator ageComparator = new AgeComparator();
if(ageComparator.compare(student1, student2) < 0) {
//若student1.age < student2.age,则打印:
System.out.println("John Smith < Don Michael");
}else if(ageComparator.compare(student1, student2) > 0) {
//若student1.age > student2.age,则打印:
System.out.println("John Smith > Don Michael");
}else {
//若student1.age == student2.age,则打印:
System.out.println("John Smith = Don Michael");
}
}
}
姓名比较器
import java.util.Comparator;
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
public static void main(String[] args) {
Student student1 = new Student("John Smith", 23);
Student student2 = new Student("Don Michael", 13);
if(ageComparator.compare(student1, student2) == 0){
//若student1.name == student2.name,则打印:
System.out.println("John Smith = Don Michael");
}else if(ageComparator.compare(student1, student2) < 0){
//若student1.age < student2.age,则打印:
System.out.println("John Smith < Don Michael");
}else {
//若student1.age > student2.age,则打印:
System.out.println("John Smith > Don Michael");
}
}
}
- 在比较器中,比较的对象可以按照需求动态修改,不是写死的
- 所以比较器
Comparator
用起来更加方便