Java Program to Empty an ArrayList in Java


ArrayLists in Java are a dynamic array who’s size can grow or shrink as elements are added or deleted from it. It is a part of the “java.util” package and is a very commonly used collection. An ArrayList is pretty much like an array as it stores elements of the same or homogeneous type in a contiguous block of memory. In this article, we are going to se the different ways to empty an ArrayList in Java.

There are mainly 4 approaches to empty an ArrayList in Java and are as follows −

  • Using the Naïve method

  • Using the clear() method

  • Using the removeAll() method

  • Assigning a new ArrayList instance

Let us now look at each approach in more detail along with its Java implementation.

1. Using the Naïve Method

This method does not make use of any in-inbuilt methods to delete elements of an ArrayList rather a while loop. The isEmpty() method returns true or false if the ArrayList is empty. The while loops keeps executing till isEmpty() returns false and inside the body the first element or element at index 0 is removed each time. Once isEmpty() returns True, the loops terminates. It can be considered as a manual and time complex method which removes each element 1 by 1 from the ArrayList.

Example

import java.util.ArrayList;
public class EmptyArrayListExample {
   public static void main(String[] args) {
   
      ArrayList<String> list_of_languages = new ArrayList<String>();
   
      // Fill the ArrayList
      list_of_languages.add("Java");
      list_of_languages.add("Python");
      list_of_languages.add("C++");
      list_of_languages.add("R");
      list_of_languages.add("C#");
      list_of_languages.add("JavaScript");
   
      System.out.println("Original Arraylist: " + list_of_languages);
   
      // run a while loop till it becomes empty
      while (!list_of_languages.isEmpty()) {
         list_of_languages.remove(0); // remove the first element
      }
      // print the ArrayList after attempting to empty it
      System.out.println("Empty Arraylist: " + list_of_languages);
   }
}

Output

The above program will produce the following output -

Original Arraylist: [Java, Python, C++, R, C#, JavaScript]
Empty Arraylist: []

2. Using the clear() Method

This approach involves the use of the ArrayList class’s in-built clear() method which doesn’t take in any parameters or return any values. It is the easiest way to empty an ArrayList.

Example

import java.util.ArrayList;
public class EmptyArrayListExample {
   public static void main(String[] args) {
      // Fill the arraylist
      ArrayList<String> list_of_languages = new ArrayList<String>();
   
      // Add some elements to the list_of_languages
      list_of_languages.add("Java");
      list_of_languages.add("Python");
      list_of_languages.add("C++");
      list_of_languages.add("R");
      list_of_languages.add("C#");
      list_of_languages.add("JavaScript");
        
      // Print the original list_of_languages
      System.out.println("Original Arraylist: " + list_of_languages);
   
      // Use the clear() method to empty the list_of_languages
      list_of_languages.clear();
   
      // Print the empty list_of_languages
      System.out.println("Empty Arraylist: " + list_of_languages);
   }
}

Output

The above program will produce the following output -

Original Arraylist: [Java, Python, C++, R, C#, JavaScript]
Empty Arraylist: []

3. Using the removeAll() Method

The removeAll() method is also in-built in the ArrayList class. This method can take in an optional parameter which is another collection or set of elements to be removed from the ArrayList. It returns a Boolean value which is true if the list has been changed due to the function call. In case no elements are passed to it, it deletes all elements in the list.

Example

import java.util.ArrayList;
public class EmptyArrayListExample {
   public static void main(String[] args) {
   
      ArrayList<String> list_of_languages = new ArrayList<String>();
   
      // Fill the ArrayList
      list_of_languages.add("Java");
      list_of_languages.add("Python");
      list_of_languages.add("C++");
      list_of_languages.add("R");
      list_of_languages.add("C#");
      list_of_languages.add("JavaScript");
   
      System.out.println("Original Arraylist: " + list_of_languages);
   
      // Use the removeAll() method to empty the list_of_languages
      list_of_languages.removeAll(list_of_languages);
   
      System.out.println("Empty Arraylist: " + list_of_languages);
   }
}

Output

The above program will produce the following output -

Original Arraylist: [Java, Python, C++, R, C#, JavaScript]
Empty Arraylist: []

4. Assigning a new ArrayList instance

This technique involves creating a new instance of the ArrayList and assigning it to the same variable again. However, this method is not preferred even though it is valid because creates unnecessary overhead and can lead to potential memory leaks.

When we assign a new ArrayList to the instance to the same existing ArrayList variable, the old ArrayList object is being made eligible for garbage collection. The garbage collection process doesn’t happen immediately and may take some time before the old object has been removed from memory which means we have may have 2 ArrayList objects in memory and 1 of them is not being used anymore.

Furthermore, the old ArrayList object still has references to it elsewhere in the program and those references will still be pointing to the old object even though it is not being used anymore. This can be the reason of a potential memory leak causing the program to consume more memory than actually needed.

Example

import java.util.ArrayList;
public class EmptyArrayListExample {
   public static void main(String[] args) {
   
      ArrayList<String> list_of_languages = new ArrayList<String>();
   
      // Fill the ArrayList
      list_of_languages.add("Java");
      list_of_languages.add("Python");
      list_of_languages.add("C++");
      list_of_languages.add("R");
      list_of_languages.add("C#");
      list_of_languages.add("JavaScript");
   
      System.out.println("Original Arraylist: " + list_of_languages);
   
      // create and assign a new instance of the ArrayList to the old arrayList variable
      list_of_languages = new ArrayList<String>();
   
      System.out.println("Empty Arraylist: " + list_of_languages);
   }
}

Output

The above program will produce the following output -

Original Arraylist: [Java, Python, C++, R, C#, JavaScript]
Empty Arraylist: []

Conclusion

ArrayLists are dynamic sized arrays which store homogeneous data. The ArrayList class provides us with a several methods to empty an ArrayList. These include the clear() method and the removeAll() method. Apart from the inbuilt functions, there are 2 more methods to empty an ArrayList. These include the naïve approach to delete every element of the ArrayList and the assigning a new instance of ArrayList to the same variable. Out of the 4 methods mentioned it is always preferrable to make use of the clear() method. The 4th method of reassigning a new instance of the ArrayList class which does the work of emptying an ArrayList, can be potential and should be avoided. ArrayLists are commonly used in Java and more flexible as compared to an array. Overall, emptying an ArrayList is a simple task.

Updated on: 10-Mar-2023

344 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements