So Close, Yet So Far. (iterators, next, hasnext) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

So Close, Yet So Far. (iterators, next, hasnext)

I am so close to finishing the Java course, but I cannot get past the 2nd to last question. The question: What does the following code output? ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 6; i++) { list.add(i); } int x = 0; Iterator<Integer> it = list.iterator(); while (it.hasNext()) { x+= it.next(); } System.out.println(x); The Problem: I've gotten a few legitimate answers and I have plugged them in, but all of them were incorrect.

30th Mar 2017, 1:17 AM
Mister Ammaiu
Mister Ammaiu - avatar
4 Answers
+ 10
This code adds the numbers 0 to 5 in a list and adds them, so you get 0 + 1 + 2 + 3 + 4 + 5 = 15.
30th Mar 2017, 1:25 AM
Karl T.
Karl T. - avatar
+ 1
15
30th Mar 2017, 1:23 AM
Mister Ammaiu
Mister Ammaiu - avatar
0
Nevermind. I got the answer.
30th Mar 2017, 1:22 AM
Mister Ammaiu
Mister Ammaiu - avatar
0
15
17th Jul 2019, 9:55 PM
Sounak dey
Sounak dey - avatar