Documentation

The Java™ Tutorials
Hide TOC
Questions and Exercises问题和练习
Trail: Learning the Java Language
Lesson: Generics (Updated)

Questions and Exercises: Generics问题和练习:泛型

  1. Write a generic method to count the number of elements in a collection that have a specific property (for example, odd integers, prime numbers, palindromes).编写一个泛型方法来计算集合中具有特定属性(例如,奇数整数、素数、回文)的元素数。
  2. Will the following class compile? If not, why? 下面的类将编译吗?若否,原因为何?
    public final class Algorithm {
        public static <T> T max(T x, T y) {
            return x > y ? x : y;
        }
    }
  3. Write a generic method to exchange the positions of two different elements in an array.编写一个通用方法来交换数组中两个不同元素的位置。
  4. If the compiler erases all type parameters at compile time, why should you use generics?如果编译器在编译时删除所有类型参数,为什么要使用泛型?
  5. What is the following class converted to after type erasure? 类型擦除后,以下类转换为什么?
    public class Pair<K, V> {
    
        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }
    
        public K getKey(); { return key; }
        public V getValue(); { return value; }
    
        public void setKey(K key)     { this.key = key; }
        public void setValue(V value) { this.value = value; }
    
        private K key;
        private V value;
    }
  6. What is the following method converted to after type erasure?
    public static <T extends Comparable<T>> int findFirstGreaterThan(T[] at, T elem) {
        // ...
    }
  7. Will the following method compile? If not, why?
    public static void print(List<? extends Number> list) {
        for (Number n : list)
            System.out.print(n + " ");
        System.out.println();
    }
  8. Write a generic method to find the maximal element in the range [begin, end) of a list.
  9. Will the following class compile? If not, why?
    public class Singleton<T> {
    
        public static T getInstance() {
            if (instance == null)
                instance = new Singleton<T>();
    
            return instance;
        }
    
        private static T instance = null;
    }
  10. Given the following classes:
    class Shape { /* ... */ }
    class Circle extends Shape { /* ... */ }
    class Rectangle extends Shape { /* ... */ }
    
    class Node<T> { /* ... */ }
    Will the following code compile? If not, why?
    Node<Circle> nc = new Node<>();
    Node<Shape> ns = nc;
  11. Consider this class:
    class Node<T> implements Comparable<T> {
        public int compareTo(T obj) { /* ... */ }
        // ...
    }
    Will the following code compile? If not, why?
    Node<String> node = new Node<>();
    Comparable<String> comp = node;
  12. How do you invoke the following method to find the first integer in a list that is relatively prime to a list of specified integers?
    public static <T> int findFirst(List<T> list, int begin, int end, UnaryPredicate<T> p)
    Note that two integers a and b are relatively prime if gcd(a, b) = 1, where gcd is short for greatest common divisor.
Check your answers.

Previous page: Restrictions on Generics
Next page: Packages