Empty an ArrayList in Java



ArrayLists in Java are a dynamic array whose 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 see the different ways to empty an ArrayList in Java.

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

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 than a while loop. The isEmpty() method returns true or false if the ArrayList is empty. The while loops keep 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 loop terminates. It can be considered a manual and time-complex method that 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 built-in 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.

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 that store homogeneous data. The ArrayList class provides us with 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 deleting every element of the ArrayList and assigning a new instance of ArrayList to the same variable. 

Updated on: 2024-12-24T17:55:45+05:30

555 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements