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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
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

Post 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 18 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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply