Developing Algorithms Using ArrayLists

AP Computer Science A· difficulty 3/5

ArrayList<Integer> a = new ArrayList<>();
a.add(1);
a.add(2);
a.add(3);
ArrayList<Integer> b = new ArrayList<>();
for (int i = a.size() - 1; i >= 0; i--) b.add(a.get(i));
System.out.println(b);

What is printed?

  • A

    []

  • B

    [1, 2, 3]

  • C

    [3, 2, 1]

    check_circle
  • D

    [3, 2, 1, 0]

Explanation

Iterating from the last index to 0 and appending to b reverses the list.

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

Related questions