An ArrayList is declared by the following code Assume that c
An ArrayList is declared by the following code:
Assume that cities has been initialized and 3 values have been added to the ArrayList. Write only the code that will perform the following operations on this ArrayList
insert \"Atlanta\" as the second item in this List
remove the last item in the list
print all the items in the list without using a loop
:
Solution
import java.util.ArrayList;
public class Test{
public static void main(String[] args){
ArrayList<String> cities = new ArrayList<String>();
cities.add(\"New York\");
cities.add(\"London\");
cities.add(\"Dallas\");
// insert \"Atlanta\" as the second item in this List
cities.add(1, \"Atlanta\");
// remove the last item in the list
cities.remove(cities.size() - 1);
// print all the items in the list without using a loop
System.out.println(cities);
}
}
