+ 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??
5 Respostas
+ 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.
+ 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
+ 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
+ 3
5éĄéĄçźçéžæéçšïŒćæäżŸéŻççæĄć
„ć°ćéĄćș«ç