1. public static <T extends Comparable> T min(T[] a)...
    1. package chapter08;
    2. public class ArrayAlg {
    3. public static <T> T getMiddle(T... a) {
    4. return a[a.length/2];
    5. }
    6. public static <T extends Comparable> T min(T[] a) {
    7. if (a == null || a.length == 0) return null;
    8. T smallest = a[0];
    9. for (int i = 1; i < a.length; i++) {
    10. if (smallest.compareTo(a[i]) > 0) {
    11. smallest = a[i];
    12. }
    13. }
    14. return smallest;
    15. }
    16. public static void main(String[] args) {
    17. String a = ArrayAlg.<String>getMiddle("abc", "def");
    18. System.out.println(a);
    19. }
    20. }

    使用多个限定:

    1. T extends Comparable & Serializable