Back to questions
JuniorTheoryCommon
What does the program print?
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> xs = new ArrayList<>();
xs.add(10);
xs.add(20);
xs.add(30);
xs.remove(1);
System.out.println(xs);
}
}[10, 30]
- Expecting
[10, 20, 30]:remove(1)with anintremoves by index, not by value; to remove the number1, callremove(Integer.valueOf(1)). - Expecting
[20, 30]: list indices start at 0, as in arrays, so index 1 is the second element,20.
- How do you remove the value
20instead of the element at an index? - What does
remove(Object)return when the value is not in the list?