Traversing ArrayLists

AP Computer Science A· difficulty 3/5

Sum all integers in <code>ArrayList<Integer> list</code>:

  • A

    for (int x : list) sum += x;

    check_circle
  • B

    Both work; first is enhanced-for, second uses size()

  • C

    for (int i = 0; i < list.length; i++) sum += list[i];

  • D

    Cannot iterate

Explanation

First works (with auto-unboxing); second has wrong syntax (use <code>list.size()</code> and <code>list.get(i)</code>).

Want 10 more like this — adaptive to your weak spots?

Related questions