(一)自然排序 java.lang.Comparable
代码:
package java.lang;
public interface Comparable{
int compareTo(Object obj);
}
package com.atguigu.api;
public class Student implements Comparable {
private int id;
private String name;
private int score;
private int age;
public Student(int id, String name, int score, int age) {
this.id = id;
this.name = name;
this.score = score;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", score=" + score +
", age=" + age +
'}';
}
@Override
public int compareTo(Object o) {
//这些需要强制,将 o 对象向下转型为 Student 类型的变量,才能调用 Stu
dent 类中的属性
//默认按照学号比较大小
Student stu = (Student) o;
return this.id - stu.id;
}
}
测试类:
package com.atguigu.api;
public class TestStudent {
public static void main(String[] args) {
Student[] arr = new Student[5];
arr[0] = new Student(3,"张三",90,23);
arr[1] = new Student(1,"熊大",100,22);
arr[2] = new Student(5,"王五",75,25);
arr[3] = new Student(4,"李四",85,24);
arr[4] = new Student(2,"熊二",85,18);
//单独比较两个对象
System.out.println(arr[0].compareTo(arr[1]));
System.out.println(arr[1].compareTo(arr[2]));
System.out.println(arr[2].compareTo(arr[2]));
System.out.println("所有学生:");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("按照学号排序:");
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length-i; j++) {
if(arr[j].compareTo(arr[j+1])>0){
Student temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
(二)定制排序:java.util.Comparator
package java.util;
public interface Comparator{
int compare(Object o1,Object o2);
}
具体代码:文章来源:https://www.toymoban.com/news/detail-438582.html
package com.atguigu.api;
import java.util.Comparator;
//定义定制比较器类
public class StudentScoreComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
int result = s1.getScore() - s2.getScore();
return result != 0 ? result : s1.getId() - s2.getId();
}
}
测试类:文章来源地址https://www.toymoban.com/news/detail-438582.html
package com.atguigu.api;
public class TestStudent {
public static void main(String[] args) {
Student[] arr = new Student[5];
arr[0] = new Student(3, "张三", 90, 23);
arr[1] = new Student(1, "熊大", 100, 22);
arr[2] = new Student(5, "王五", 75, 25);
arr[3] = new Student(4, "李四", 85, 24);
arr[4] = new Student(2, "熊二", 85, 18);
System.out.println("所有学生:");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("按照成绩排序");
StudentScoreComparator sc = new StudentScoreComparator();
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length - i; j++) {
if (sc.compare(arr[j], arr[j + 1]) > 0) {
Student temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
到了这里,关于Java ---比较器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!