SWE 619 In Class Exercise # 12B: Generifying methods


Name(s):

Consider Bloch's min example:
    public static <???> T min (List<???> list) {
       Iterator<T> itr = list.iterator();
       if (itr.hasNext() == false) {
          throw new IllegalArgumentException("min: Empty list");
       }

       T result = itr.next();

       while (itr.hasNext()) {
           T comp = itr.next();
           if (comp.compareTo(result) < 0) {    // throws NPE, CCE as needed
               result = comp;
           }
       }
       return result;
    }
}
  1. Generify with a recursive type declaration

  2. Write a contract - be sure to consider all of the edge cases

  3. Bloch's code has a bug; write a test that finds it. Fix the code

  4. Does a bounded wildcard make this method more general? Can you think of some other way to generalize this method?