We want this code to print the titles of each book twice.
Why doesn't it?
LinkedList<String> list = new LinkedList<>(); list.add("MyClass"); list.add("1984"); list.forEach(System.out::println); Iterator it = list.iterator(); while (it.hasMore()) System.out.println(it.next());
hasMore()
method should be changed to hasNext()
.forEach()
since the stream is used up.B.
The Iterator interface uses the hasNext()
and next()
methods to iterate.
Since there is not a hasMore()
method, it should be changed to hasNext()
, making Option B the answer.
With respect to Option A, the missing generic type gives a warning, but the code still runs.
For Option C, iterators can run as many times as you want, as can the forEach()
method on list.