
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Java Program to Iterate over an ArrayList
- Java Program to Remove Repeated Element from An ArrayList
- Clear an ArrayList in Java
- Clone an ArrayList in Java
- Initialize an ArrayList in Java
- How to reverse an ArrayList in Java?
- How to synchronize an ArrayList in Java?
- Convert an ArrayList to HashSet in Java
- Program to convert ArrayList to LinkedList in Java
- How to empty an array in Java
- Sort Elements in an ArrayList in Java
- How to create an ArrayList from an Array in Java?
- How to replace an element of an ArrayList in Java?
- Java Program to Find Common Elements in Two ArrayList
- Search an element of ArrayList in Java
