import java.util.Arrays; import java.util.Comparator; public class ArrarysCommonMethods { public static void main(String[] args) { // 默认实现如何实现定制排序的 // 使用冒泡法完成 int myarr[] = { 1, -1, 4, 3, 9, -2, 8 }; MyArrys.sort(myarr, new MyComparator() { @Override public int compare(int n1, int n2) { return n2-n1; } }); System.out.println("自己使用冒泡写的定制排序:" + Arrays.toString(myarr)); } } interface MyComparator { public int compare(int n1, int n2); } class MyArrys { // 静态方法 public static void sort(int[] arr, MyComparator comparator) { // 冒泡 int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { // 判断 if (comparator.compare(arr[j], arr[j + 1]) > 0) { // 交换 temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/java-jiao-cheng/11787.html