此泛型方法使用选择排序和输入比较器对象对输入数组进行排序。
//package com.nowjava; /** 来自 nowjava.com - 时 代 Java**/ import java.util.Comparator; public class Main { /** * This generic method sorts the input array using a selection sort and the input Comparator object. * @param arr * @param c */ public static <T> void selectionSort(T[] arr, Comparator<? super T> c) { // validate parameters if (arr == null) { throw new IllegalArgumentException( "The given array cannot be null!"); } if (c == null) { throw new IllegalArgumentException( "The given comparator cannot be null!"); } int smallestIndex; for (int i = 0; i < arr.length - 1; i++) { smallestIndex = i; /** from nowjava - 时 代 Java**/ // Find the index of the smallest element for (int j = i + 1; j < arr.length; j++) { if (c.compare(arr[j], arr[smallestIndex]) < 0) { smallestIndex = j;