From 8e2ce1ec599f177098ca0c15d08f88aa7f1ecce1 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Mon, 3 Aug 2026 14:24:06 +0800 Subject: [PATCH] =?UTF-8?q?s-day04-=E6=8E=A5=E5=8F=A3=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- s-day04/src/com/inmind/sort05/SortDemo12.java | 79 +++++++++++++++++-- s-day04/src/com/inmind/treeset02/Student.java | 4 +- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/s-day04/src/com/inmind/sort05/SortDemo12.java b/s-day04/src/com/inmind/sort05/SortDemo12.java index 7b97f8f..1df2bbf 100644 --- a/s-day04/src/com/inmind/sort05/SortDemo12.java +++ b/s-day04/src/com/inmind/sort05/SortDemo12.java @@ -1,27 +1,96 @@ package com.inmind.sort05; -import java.util.Arrays; +import com.inmind.treeset02.Student; + +import java.util.*; /* 冒泡排序 + +接口回调:帮助大家理解比较器Comparator,如果实现排序功能 + +接口回调的原理功能:将我们自定义的业务逻辑,嵌入到源码中!!!! */ public class SortDemo12 { public static void main(String[] args) { //创建一个数组 - int[] arr = {1, 5, 3, 4, 2}; + Integer[] arr = {1, 5, 3, 4, 2}; //5个数字,要排4趟 + sort(arr,new Comparator(){ + + @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() { + @Override + public int compare(Student o1, Student o2) { + return o2.age - o1.age; + } + }); + System.out.println(Arrays.toString(students)); + System.out.println("----------------------"); + ArrayList 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() { + @Override + public int compare(Student o1, Student o2) { + return o2.age - o1.age; + } + }); + System.out.println(list); + } + + //定义一个排序方法,接收一个需要排序的整数数组,和一个比较器Comparator + public static void sort(T[] arr, Comparator comparator) { for (int i = 0; i < arr.length - 1; i++) {//循环4次 //每趟排序的次数刚好就是arr.length - 1-i 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+1] = temp; } } } - System.out.println(Arrays.toString(arr)); + } + + public static void sortList(List arr, Comparator 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); + } + } + } } } diff --git a/s-day04/src/com/inmind/treeset02/Student.java b/s-day04/src/com/inmind/treeset02/Student.java index 4be5024..1d9abd7 100644 --- a/s-day04/src/com/inmind/treeset02/Student.java +++ b/s-day04/src/com/inmind/treeset02/Student.java @@ -3,8 +3,8 @@ package com.inmind.treeset02; import java.util.Objects; public class Student implements Comparable{ - String name; - int age; + public String name; + public int age; public Student(String name, int age) { this.name = name;