public static <T extends Comparable> T min(T[] a)...
package chapter08;
public class ArrayAlg {
public static <T> T getMiddle(T... a) {
return a[a.length/2];
}
public static <T extends Comparable> T min(T[] a) {
if (a == null || a.length == 0) return null;
T smallest = a[0];
for (int i = 1; i < a.length; i++) {
if (smallest.compareTo(a[i]) > 0) {
smallest = a[i];
}
}
return smallest;
}
public static void main(String[] args) {
String a = ArrayAlg.<String>getMiddle("abc", "def");
System.out.println(a);
}
}
使用多个限定:
T extends Comparable & Serializable