Question:
How to empty an ArrayList?
Answer:
Code:
import java.util.ArrayList; public class EmptyArrayList { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("John"); list.add("Ringo"); list.add("George"); System.out.println("ArrayList: "+ list); list.add("Paul"); System.out.println("ArrayList: "+ list); list.add("Me"); list.clear(); System.out.println("ArrayList: "+ list); } }
Output:
$ java EmptyArrayList ArrayList: [John, Ringo, George] ArrayList: [John, Ringo, George, Paul] ArrayList: []