s-day04-Arrays工具类的排序功能介绍

This commit is contained in:
2026-08-03 14:46:53 +08:00
parent 8e2ce1ec59
commit 943af86cf2
2 changed files with 29 additions and 3 deletions
@@ -0,0 +1,26 @@
package com.inmind.arrays06;
import java.util.Arrays;
import java.util.Comparator;
/*
Arrays
public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
static <T> void sort(T[] a, Comparator<? super T> c) 根据指定的比较器引发的顺序对指定的对象数组进行排序。
*/
public class ArraysDemo13 {
public static void main(String[] args) {
Integer[] arr = {1, 5, 3, 9, 2, 4, 7, 6, 8};
System.out.println(Arrays.toString(arr));
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
System.out.println(Arrays.toString(arr));
}
}
@@ -51,11 +51,11 @@ public class SortDemo12 {
new Student("小王3", 17), new Student("小王3", 17),
new Student("小王4", 18), new Student("小王4", 18),
new Student("小王5", 16)); new Student("小王5", 16));
System.out.println(list);
sortList(list, new Comparator<Student>() { sortList(list, new Comparator<Student>() {
@Override @Override
public int compare(Student o1, Student o2) { public int compare(Student o1, Student o2) {
return o2.age - o1.age; return o1.age - o2.age;
} }
}); });
System.out.println(list); System.out.println(list);
@@ -88,7 +88,7 @@ public class SortDemo12 {
//交换 //交换
T temp = arr.get(j); T temp = arr.get(j);
arr.set(j, arr.get(j+1)); arr.set(j, arr.get(j+1));
arr.set(j = 1, temp); arr.set(j + 1, temp);
} }
} }
} }