Ben Bitdiddle has written some generic Java code to run an iterator, storing the results into some output list: interface Iterator<T> { bool hasNext(); T next(); } interface List<T> { void add (T); } static public <T> void copyTo (Iterator<T> it, List<T> out) { while (it.hasNext()) out.add(it.next()); } Alyssa P. Hacker has a Iterator<Integer> and would like to copy it into an array of type List<Number> (Integer is a subtype of Number). However, she discovers that she cannot do so!
(c) The definitions of Iterator and List in the Java SE 7's standard library have more meth- ods than we have written down here. What declaration-site variance should be assigned to the full class definition of Iterator and List? (d) Although declaration-site variance is sufficient to make Ben's example work, there are some cases use-site variance (wildcards) are more expressive than declaration-site variance. Give a brief example of use-site variance which cannot be rewritten using declaration-site vari- ance. Write down any interfaces you use.
Ben Bitdiddle has written some generic Java code to run an iterator, storing the results into some output list: interfac
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Ben Bitdiddle has written some generic Java code to run an iterator, storing the results into some output list: interfac
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!