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 package
import
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)
- Using removeAll() method
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 package
import
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)