how to clear an arraylist code example

Example 1: ArrayList clear() method in java

import java.util.ArrayList;
public class ArrayListClearMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<Integer>(4);
      al.add(9);
      al.add(3);
      al.add(5);
      al.add(1);
      System.out.println("ArrayList before clear() method: " + al);
      al.clear();
      System.out.println("ArrayList after clear() method: " + al);
   }
}

Example 2: how to reset an arraylist in java

There are two ways to empty an ArrayList:
1. By using ArrayList.clear() method or with the help of ArrayList.
2. removeAll() method.

Tags:

Java Example