1.编写一个泛型方法来计算集合中具有特定属性(例如,奇数整数,素数,回文数)的元素数。

    1. public final class Algorithm {
    2. public static <T> int countIf(Collection<T> c, UnaryPredicate<T> p) {
    3. int count = 0;
    4. for (T elem : c)
    5. if (p.test(elem))
    6. ++count;
    7. return count;
    8. }
    9. }

    泛型接口UnaryPredicate的定义如下:

    1. public interface UnaryPredicate<T> {
    2. public boolean test(T obj);
    3. }

    例如,以下程序对整数列表中的奇数整数进行计数:

    1. import java.util.*;
    2. class OddPredicate implements UnaryPredicate<Integer> {
    3. public boolean test(Integer i) { return i % 2 != 0; }
    4. }
    5. public class Test {
    6. public static void main(String[] args) {
    7. Collection<Integer> ci = Arrays.asList(1, 2, 3, 4);
    8. int count = Algorithm.countIf(ci, new OddPredicate());
    9. System.out.println("Number of odd integers = " + count);
    10. }
    11. }

    程序打印:

    1. Number of odd integers = 2

    2.以下类会编译吗?如果没有,为什么?

    1. public final class Algorithm {
    2. public static <T> T max(T x, T y) {
    3. return x > y ? x : y;
    4. }
    5. }

    答案:否。大于(>)运算符仅适用于基本数字类型。
    3.编写泛型方法来交换数组中两个不同元素的位置。

    1. public final class Algorithm {
    2. public static <T> void swap(T[] a, int i, int j) {
    3. T temp = a[i];
    4. a[i] = a[j];
    5. a[j] = temp;
    6. }
    7. }

    4.如果编译器在编译时擦除所有类型参数,那么为什么要使用泛型?
    :您应该使用泛型,因为:

    • Java编译器在编译时对泛型代码执行更严格的类型检查。
    • 泛型支持将编程类型作为参数。
    • 泛型使您可以实现泛型算法。

    5.类型擦除后,以下类转换为什么?

    1. public class Pair<K, V> {
    2. public Pair(K key, V value) {
    3. this.key = key;
    4. this.value = value;
    5. }
    6. public K getKey(); { return key; }
    7. public V getValue(); { return value; }
    8. public void setKey(K key) { this.key = key; }
    9. public void setValue(V value) { this.value = value; }
    10. private K key;
    11. private V value;
    12. }

    1. public class Pair {
    2. public Pair(Object key, Object value) {
    3. this.key = key;
    4. this.value = value;
    5. }
    6. public Object getKey() { return key; }
    7. public Object getValue() { return value; }
    8. public void setKey(Object key) { this.key = key; }
    9. public void setValue(Object value) { this.value = value; }
    10. private Object key;
    11. private Object value;
    12. }

    6.类型擦除后,以下方法转换为什么?

    1. public static <T extends Comparable<T>>
    2. int findFirstGreaterThan(T[] at, T elem) {
    3. // ...
    4. }

    1. public static int findFirstGreaterThan(Comparable[] at, Comparable elem) {
    2. // ...
    3. }

    7.可以编译以下方法吗?如果没有,为什么?

    1. public static void print(List<? extends Number> list) {
    2. for (Number n : list)
    3. System.out.print(n + " ");
    4. System.out.println();
    5. }

    :可以。
    8.编写泛型方法以在列表的[begin,end)范围内找到最大元素。

    1. import java.util.*;
    2. public final class Algorithm {
    3. public static <T extends Object & Comparable<? super T>>
    4. T max(List<? extends T> list, int begin, int end) {
    5. T maxElem = list.get(begin);
    6. for (++begin; begin < end; ++begin)
    7. if (maxElem.compareTo(list.get(begin)) < 0)
    8. maxElem = list.get(begin);
    9. return maxElem;
    10. }
    11. }

    9.以下类会编译吗?如果没有,为什么?

    1. public class Singleton<T> {
    2. public static T getInstance() {
    3. if (instance == null)
    4. instance = new Singleton<T>();
    5. return instance;
    6. }
    7. private static T instance = null;
    8. }

    :不能。您不能创建类型参数T的静态字段。
    10.给定以下类别:

    1. class Shape { /* ... */ }
    2. class Circle extends Shape { /* ... */ }
    3. class Rectangle extends Shape { /* ... */ }
    4. class Node<T> { /* ... */ }

    以下代码可以编译吗?如果没有,为什么?

    1. Node<Circle> nc = new Node<>();
    2. Node<Shape> ns = nc;

    回答:否。因为Node 不是Node 的子类型。
    11.考虑此类:

    1. class Node<T> implements Comparable<T> {
    2. public int compareTo(T obj) { /* ... */ }
    3. // ...
    4. }

    以下代码可以编译吗?如果没有,为什么?

    :可以。

    1. Node<String> node = new Node<>();
    2. Comparable<String> comp = node;

    12.您如何调用以下方法在列表中查找相对于指定整数列表而言是素数的第一个整数?

    1. public static <T>
    2. int findFirst(List<T> list, int begin, int end, UnaryPredicate<T> p)

    请注意,如果gcd(a,b)= 1,则两个整数ab是相对质数,其中gcd是最大公约数的缩写。

    1. import java.util.*;
    2. public final class Algorithm {
    3. public static <T>
    4. int findFirst(List<T> list, int begin, int end, UnaryPredicate<T> p) {
    5. for (; begin < end; ++begin)
    6. if (p.test(list.get(begin)))
    7. return begin;
    8. return -1;
    9. }
    10. // x > 0 and y > 0
    11. public static int gcd(int x, int y) {
    12. for (int r; (r = x % y) != 0; x = y, y = r) { }
    13. return y;
    14. }
    15. }

    泛型接口UnaryPredicate定义如下:

    1. public interface UnaryPredicate<T> {
    2. public boolean test(T obj);
    3. }

    以下程序测试方法findFirst:

    1. import java.util.*;
    2. class RelativelyPrimePredicate implements UnaryPredicate<Integer> {
    3. public RelativelyPrimePredicate(Collection<Integer> c) {
    4. this.c = c;
    5. }
    6. public boolean test(Integer x) {
    7. for (Integer i : c)
    8. if (Algorithm.gcd(x, i) != 1)
    9. return false;
    10. return c.size() > 0;
    11. }
    12. private Collection<Integer> c;
    13. }
    14. public class Test {
    15. public static void main(String[] args) throws Exception {
    16. List<Integer> li = Arrays.asList(3, 4, 6, 8, 11, 15, 28, 32);
    17. Collection<Integer> c = Arrays.asList(7, 18, 19, 25);
    18. UnaryPredicate<Integer> p = new RelativelyPrimePredicate(c);
    19. int i = ALgorithm.findFirst(li, 0, li.size(), p);
    20. if (i != -1) {
    21. System.out.print(li.get(i) + " is relatively prime to ");
    22. for (Integer k : c)
    23. System.out.print(k + " ");
    24. System.out.println();
    25. }
    26. }
    27. }

    程序打印:

    1. 11 is relatively prime to 7 18 19 25