Open In App

Java Program to Empty an ArrayList in Java

Last Updated : 14 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 :

  1. Using clear() method.
  2. 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");
    }
}


Output

Numbers 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");
    }
}


Output

Numbers 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads