0

why is out is 15

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);

3rd May 2020, 10:09 AM
ABUBAKAR AHMED ALI
ABUBAKAR AHMED ALI - avatar
1 Answer
+ 2
ABUBAKAR AHMED ALI , the first loop adds the following values in the arraylist => 0, 1, 2, 3, 4, 5. The second loop calculates the sum of this elements 0+1+2+3+4+5 => 15.
3rd May 2020, 10:29 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar