ArrayList Clear examples in Java | Java ArrayList

Looking for how to make an ArrayList empty following ArrayList example program will Remove all of the elements from ArrayList. The list will be empty after calling the clear method. ArrayList clear method has been implemented from the Collection interface and overridden from AbstractList.

Arraylistclear.java


import java.util.ArrayList;
public class ArrayListClear

{
 public static void main(String[] args)
 {

  ArrayList < Integer > al = new ArrayList < Integer > ();

  al.add(23);
  al.add(47);
  al.add(72);
  al.add(10);
  al.add(7);
  al.add(92);
  al.add(58);
  al.add(69);
  al.add(92);

  System.out.println(al);
  al.clear();
  System.out.println(al);
 }
}

Output


[23, 47, 72, 10, 7, 92, 58, 69, 92]

[]

Leave a Reply

Your email address will not be published. Required fields are marked *