s-day04-接口回调案例

This commit is contained in:
2026-08-03 14:24:06 +08:00
parent f0ec8dfee1
commit 8e2ce1ec59
2 changed files with 76 additions and 7 deletions
+74 -5
View File
@@ -1,27 +1,96 @@
package com.inmind.sort05; package com.inmind.sort05;
import java.util.Arrays; import com.inmind.treeset02.Student;
import java.util.*;
/* /*
冒泡排序 冒泡排序
接口回调:帮助大家理解比较器Comparator,如果实现排序功能
接口回调的原理功能:将我们自定义的业务逻辑,嵌入到源码中!!!!
*/ */
public class SortDemo12 { public class SortDemo12 {
public static void main(String[] args) { public static void main(String[] args) {
//创建一个数组 //创建一个数组
int[] arr = {1, 5, 3, 4, 2}; Integer[] arr = {1, 5, 3, 4, 2};
//5个数字,要排4趟 //5个数字,要排4趟
sort(arr,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
//return o1-o2;//升序
return o2-o1;
}
});
System.out.println(Arrays.toString(arr));
System.out.println("--------------------------");
Student[] students = {
new Student("小王1", 15),
new Student("小王2", 19),
new Student("小王3", 17),
new Student("小王4", 18),
new Student("小王5", 16)
};
sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.age - o1.age;
}
});
System.out.println(Arrays.toString(students));
System.out.println("----------------------");
ArrayList<Student> list = new ArrayList<>();
Collections.addAll(list,
new Student("小王1", 15),
new Student("小王2", 19),
new Student("小王3", 17),
new Student("小王4", 18),
new Student("小王5", 16));
sortList(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.age - o1.age;
}
});
System.out.println(list);
}
//定义一个排序方法,接收一个需要排序的整数数组,和一个比较器Comparator
public static <T> void sort(T[] arr, Comparator<T> comparator) {
for (int i = 0; i < arr.length - 1; i++) {//循环4次 for (int i = 0; i < arr.length - 1; i++) {//循环4次
//每趟排序的次数刚好就是arr.length - 1-i //每趟排序的次数刚好就是arr.length - 1-i
for (int j = 0; j < arr.length - 1 - i; j++) { for (int j = 0; j < arr.length - 1 - i; j++) {
//每趟中每次都是相邻比较,如果大或者小就往后(升序,降序) //每趟中每次都是相邻比较,如果大或者小就往后(升序,降序)
if (arr[j] < arr[j + 1]) { // if (arr[j] -arr[j + 1] > 0 ) {
if (comparator.compare(arr[j], arr[j + 1]) > 0 ) {
//交换 //交换
int temp = arr[j]; T temp = arr[j];
arr[j] = arr[j + 1]; arr[j] = arr[j + 1];
arr[j+1] = temp; arr[j+1] = temp;
} }
} }
} }
System.out.println(Arrays.toString(arr)); }
public static <T> void sortList(List<T> arr, Comparator<T> comparator) {
for (int i = 0; i < arr.size() - 1; i++) {//循环4次
//每趟排序的次数刚好就是arr.length - 1-i
for (int j = 0; j < arr.size() - 1 - i; j++) {
//每趟中每次都是相邻比较,如果大或者小就往后(升序,降序)
// if (arr[j] -arr[j + 1] > 0 ) {
if (comparator.compare(arr.get(j), arr.get(j+1)) > 0 ) {
//交换
T temp = arr.get(j);
arr.set(j, arr.get(j+1));
arr.set(j = 1, temp);
}
}
}
} }
} }
@@ -3,8 +3,8 @@ package com.inmind.treeset02;
import java.util.Objects; import java.util.Objects;
public class Student implements Comparable<Student>{ public class Student implements Comparable<Student>{
String name; public String name;
int age; public int age;
public Student(String name, int age) { public Student(String name, int age) {
this.name = name; this.name = name;