+ 1

code question

What is the output of this code? List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); Iterator<Integer> it = list.iterator(); it.next(); System.out.println(it.next()); why the ans is 20?? is the ans wrong??

14th Dec 2018, 7:03 AM
林冠渝
林冠渝 - avatar
5 ответов
+ 5
@bullion is right. Every time you call the it.next() method, it iterates through the list internally that is why you get 20 instead of 10.
14th Dec 2018, 7:48 AM
Lambda_Driver
Lambda_Driver - avatar
14th Dec 2018, 8:28 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 4
No, It points to 10 when declared The first next - return 10 - move pointer to 20 The second next - return 20 - move pointer to 30
14th Dec 2018, 4:44 PM
Gordon
Gordon - avatar
+ 3
First call to it.next() makes the iterator point to 10. The second call, inside println, makes it point to 20 and it is printed
14th Dec 2018, 7:20 AM
bullion
+ 3
5題題目的選擇過程,唔會俾錯的答案入到問題庫的
14th Dec 2018, 4:45 PM
Gordon
Gordon - avatar