Page 1 of 1

Ben Bitdiddle has written some generic Java code to run an iterator, storing the results into some output list: interfac

Posted: Mon May 09, 2022 5:56 am
by answerhappygod
Ben Bitdiddle Has Written Some Generic Java Code To Run An Iterator Storing The Results Into Some Output List Interfac 1
Ben Bitdiddle Has Written Some Generic Java Code To Run An Iterator Storing The Results Into Some Output List Interfac 1 (58.88 KiB) Viewed 20 times
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.