Posts Tagged Collections

Java – How to Clear ArrayList

Monday, July 6th, 2009

All Elements in ArrayList can be removed by clear() method. Clear() method in ArrayList clear arraylist object and size of the arraylist will become 0

Example of clear in Arraylist

import java.util.ArrayList;

public class ArrayListClearExample {

    public static void main(String[] args) {

        ArrayList<String> arlist=new ArrayList<String>();

        arlist.add("First Element");
        arlist.add("Second Element");

        arlist.clear(); // clear all elements from arraylist

        System.out.println("ArrayList Size :"+arlist.size());
    }
}

Output

ArrayList Size :0