ArrayList Methods

AP Computer Science A· difficulty 3/5

ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
System.out.println(list);

What is printed?

  • A

    [A, X, C]

  • B

    [X, A, B, C]

  • C

    [A, B, X, C]

  • D

    [A, X, B, C]

    check_circle

Explanation

add(1, "X") inserts "X" at index 1, shifting "B" and "C" to the right. The result is [A, X, B, C].

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

Related questions