+ 5

Quiz Module 6

Question 5 of the 6th module's answer is 15, I don't understand why, because the condition is from i=0 to i<6. What is the output of this code? 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);

12th May 2017, 8:57 PM
Andrea Moncho
Andrea Moncho - avatar
3 Respuestas
+ 17
This code sums all the values of the list: 0+1+2+3+4+5=15 I added some explanatory comments to the code: https://code.sololearn.com/c8JXn2fMS7AP/?ref=app
12th May 2017, 9:14 PM
Tashi N
Tashi N - avatar
+ 11
You're welcome ^^
12th May 2017, 10:58 PM
Tashi N
Tashi N - avatar
+ 9
@Tashi N's excellent explanation should be enough unless there's need for translation. Thanks Tashi!
12th May 2017, 10:57 PM
Tusiime Innocent Boub
Tusiime Innocent Boub - avatar