Remove all elements from the ArrayList in Java

11 Dec, 2018
Prerequisite: ArrayList in Java
Given an ArrayList, the task is to remove all elements of the ArrayList in Java.
Examples:
Input: ArrayList = [1, 2, 3, 4] 
Output: ArrayList = [] 

Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] 
Output: ArrayList = [] 

Using clear() method:Syntax:collection_name.clear();Code of clear() method:public void clear() {
    for (int i = 0; i < size; i++)
        list[i] = null;

    size = 0;
}
Below is the implementation of the above approach:// Java Program for remove all elements ArrayList// Using clear() method  // import  ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {        // create empty ArrayList        ArrayList<String> list = new ArrayList<>();          // Adding elements of list        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("Gaurav");          // printing initial value ArrayList        System.out.println("ArrayList: " + list);          // print size of ArrayList        System.out.println("Size of ArrayList = "                           + list.size());          // remove all elements using clear() method        list.clear();          // printing ArrayList        System.out.println("\nAfter clear\n\n"                         + "ArrayList: " + list);          // print size of ArrayList after clear list        System.out.println("Size of ArrayList = "                           + list.size());    }}Output:ArrayList: [Geeks, for, Geeks, Gaurav]
Size of ArrayList = 4

After clear

ArrayList: []
Size of ArrayList = 0
Time Complexity: O(N)
Syntax:
collection_name.clear();
Code of clear() method:
public void clear() {
    for (int i = 0; i < size; i++)
        list[i] = null;

    size = 0;
}

Below is the implementation of the above approach:
// Java Program for remove all elements ArrayList// Using clear() method  // import  ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {        // create empty ArrayList        ArrayList<String> list = new ArrayList<>();          // Adding elements of list        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("Gaurav");          // printing initial value ArrayList        System.out.println("ArrayList: " + list);          // print size of ArrayList        System.out.println("Size of ArrayList = "                           + list.size());          // remove all elements using clear() method        list.clear();          // printing ArrayList        System.out.println("\nAfter clear\n\n"                         + "ArrayList: " + list);          // print size of ArrayList after clear list        System.out.println("Size of ArrayList = "                           + list.size());    }}
ArrayList: [Geeks, for, Geeks, Gaurav]
Size of ArrayList = 4

After clear

ArrayList: []
Size of ArrayList = 0

Time Complexity: O(N)
Using removeAll() methodSyntax:collection_name.removeAll(collection_name);Code of removeAll() method:public boolean removeAll(Collection list) {
    boolean isModi = false;
    Iterator ite= iterator();
    while (ite.hasNext()) {
        if (list.contains(ite.next())) {
            ite.remove();
            isModi = true;
        }
    }
    return isModi;
}
Below is the implementation of the above approach:// Java Program for remove all elements ArrayList// Using removeAll() method  // import  ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {        // create empty ArrayList        ArrayList<String> list = new ArrayList<>();          // Adding elements of list        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("Gaurav");          // printing initial value ArrayList        System.out.println("ArrayList: " + list);          // print size of ArrayList        System.out.println("Size of ArrayList = "                           + list.size());          // remove all elements using clear() method        list.removeAll(list);          // printing ArrayList        System.out.println("\nAfter clear\n\n"                         + "ArrayList: " + list);          // print size of ArrayList after clear list        System.out.println("Size of ArrayList = "                           + list.size());    }}Output:ArrayList: [Geeks, for, Geeks, Gaurav]
Size of ArrayList = 4

After clear

ArrayList: []
Size of ArrayList = 0
Time Complexity: O(N^2)My Personal Notes
arrow_drop_upSave
Syntax:
collection_name.removeAll(collection_name);
Code of removeAll() method:
public boolean removeAll(Collection list) {
    boolean isModi = false;
    Iterator ite= iterator();
    while (ite.hasNext()) {
        if (list.contains(ite.next())) {
            ite.remove();
            isModi = true;
        }
    }
    return isModi;
}

Below is the implementation of the above approach:
// Java Program for remove all elements ArrayList// Using removeAll() method  // import  ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {        // create empty ArrayList        ArrayList<String> list = new ArrayList<>();          // Adding elements of list        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("Gaurav");          // printing initial value ArrayList        System.out.println("ArrayList: " + list);          // print size of ArrayList        System.out.println("Size of ArrayList = "                           + list.size());          // remove all elements using clear() method        list.removeAll(list);          // printing ArrayList        System.out.println("\nAfter clear\n\n"                         + "ArrayList: " + list);          // print size of ArrayList after clear list        System.out.println("Size of ArrayList = "                           + list.size());    }}
ArrayList: [Geeks, for, Geeks, Gaurav]
Size of ArrayList = 4

After clear

ArrayList: []
Size of ArrayList = 0

Time Complexity: O(N^2)
Java-ArrayList
Java-Collections
Java-List-Programs
Technical Scripter 2018
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Multidimensional Arrays in Java
Stream In Java
Singleton Class in Java
Set in Java