0

How would this code (shown below), output 15?

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); how does x=15 in the end??

8th Jun 2017, 4:23 AM
Nitya Arora
Nitya Arora - avatar
1 Answer
+ 5
First loop adds numbers 0-5 to a list. Next itterates through each element in the list, adding them together. 0+1+2+3+4+5 = 15
8th Jun 2017, 4:37 AM
Rrestoring faith
Rrestoring faith - avatar