Java Program to Empty an ArrayList in Java
Last Updated :
14 Dec, 2021
ArrayList class is a resizable array, present in 'java.util package'. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can be added/appended or removed from an ArrayList without the need to create a new array.
Approaches :
- Using clear() method.
- Using removeAll() method.
Method 1: Using clear() method as the clear() method of ArrayList in Java is used to remove all the elements from an ArrayList. The ArrayList will be completely empty after this call returns.
Syntax:
public void clear() ;
Parameters: clear function takes no parameter
Return Value: This method does not return any value.
Exception: NA
Example:
Java
// Java Program to empty an ArrayList in Java
// Illustrating clear function
import java.util.ArrayList;
public class GFG {
// Main driver method
public static void main(String[] arg)
{
// Create an empty array list
// with an initial capacity of 4
ArrayList<String> numbers
= new ArrayList<String>(4);
// Use add() method to add elements
// in numbers ArrayList
numbers.add("10");
numbers.add("20");
numbers.add("30");
numbers.add("40");
// Printing numbers ArrayList
System.out.println("Numbers ArrayList : "
+ numbers);
// Finding size of numbers ArrayList
int numbers_size = numbers.size();
// Display message
System.out.println("Numbers ArrayList consists of: "
+ numbers_size + " elements");
// Display Message to between changes made in
// ArrayList
// System.out.println("Performing clear operation by
// using clear function");
// Using clear function
numbers.clear();
int numbers_size_new = numbers.size();
// Printing new ArrayList
System.out.println(
"Finally Numbers ArrayList consists of: "
+ numbers_size_new + " elements");
}
}
OutputNumbers ArrayList : [10, 20, 30, 40]
Numbers ArrayList consists of 4 elements
Performing clear operation by using clear function
Finally Numbers ArrayList consists of 0 elements
Method 2: Using removeAll() method as this method of ArrayList class is used to remove from this list all of its elements that are contained in the specified collection.
Syntax:
public boolean removeAll(Collection c) ;
Parameters: This method takes collection c as a parameter containing elements to be removed from this list.
Return Value: This method returns true if this list changed as a result of the call.
Exception/s: This method throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.
Example:
Java
// Java Program to empty an ArrayList in Java
// Java code to illustrate removeAll function
// Importing ArrayList library
import java.util.ArrayList;
public class GFG {
// Main driver method
public static void main(String[] arg)
{
// Create an empty array list
// with an initial capacity of 4
ArrayList<String> numbers
= new ArrayList<String>(4);
// Using add() method to add elements in numbers
// ArrayList
numbers.add("10");
numbers.add("20");
numbers.add("30");
numbers.add("40");
// Printing numbers ArrayList
System.out.println("Numbers ArrayList : "
+ numbers);
// Finding size of numbers ArrayList
int numbers_size = numbers.size();
// Display message
System.out.println("Numbers ArrayList consists of "
+ numbers_size + " elements");
// Display Message
System.out.println(
"Performing clear operation by using clear function");
// Using removeAll function
numbers.removeAll(numbers);
// Displaying final ArrayList count of elements
int numbers_size_new = numbers.size();
System.out.println(
"Finally Numbers ArrayList consists of "
+ numbers_size_new + " elements");
}
}
OutputNumbers ArrayList : [10, 20, 30, 40]
Numbers ArrayList consists of 4 elements
Performing clear operation by using clear function
Finally Numbers ArrayList consists of 0 elements
Similar Reads
How to remove an element from ArrayList in Java? ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. With
4 min read
Remove all elements from the ArrayList in Java 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
2 min read
Arraylist removeRange() Method in Java with Examples The removeRange() method of the ArrayList class in Java is used to remove all elements from the list within the specified range of indices. This method removes the elements from the starting index (fromIndex) to the ending index (toIndex). Example 1: Here, we use the removeRange() method to remove a
3 min read
ArrayList removeAll() Method in Java with Examples The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList that are specified in another collection or within the same list itself.Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argument
3 min read
ArrayList removeIf() Method in Java with Examples The Java ArrayList removeIf() method is used to remove all elements from the ArrayList that satisfy a given predicate filter. The predicate is passed as a parameter to the method, and any runtime exceptions thrown during iteration or by the predicate are passed to the caller.The removeIf() method us
2 min read
Remove repeated elements from ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove repeated elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 2, 3, 4, 4, 4] Output: [1, 2, 3, 4] Input: ArrayList = [12, 23, 23, 34, 45, 45, 45, 45, 57, 67, 89] Output: [12, 23, 34, 45, 57, 67, 89] Below are
3 min read