coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

ArrayList clear() method in Java

In this article, we will see the clear() method of ArrayList in Java. This method is used to remove all the elements of the ArrayList.

Introduction

We use ArrayList in day-to-day Java programming. ArrayList is an ordered collection and it allows duplicate elements.

We have seen a few methods of the ArrayList class before like add(), addAll(), remove(), set() and iterate()

Today we will see the clear() method of the ArrayList class in Java.

ArrayList clear() method in java

It has a method signature as given below.

1
public void clear()

When we invoke the clear() method on an ArrayList, it removes all of the elements from this list.

So, the list will be empty after the successful invocation of the clear method on the list.

It has a return type as a void. This means this method does not return anything.

Also, it does not accept any parameter.

Let’s see an example Java program that uses the clear() method to remove all the elements of the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.List;

/**
 * A Java program to remove all the elements 
 * of ArrayList using the clear() method.
 * 
 * @author coderolls.com
 *
 */
public class ArrayListClearMethodExample {

  public static void main(String[] args) {
  
    //Create an ArrayList cities
    List<String> cities = new ArrayList<String>();
    
    // add string objects in 'cities'
    cities.add("New York City");
    cities.add("Los Angeles");
    cities.add("Mountain View");
    cities.add("Austin");
    cities.add("Atlanta");
    
    // printing the cities
    System.out.println("Before invoking the clear() method");
    System.out.println("Cities: "+ cities); //print the ArrayList with elements
    System.out.println("cities size: "+ cities.size());
    
    //invoke the clear() method on arraylist
    cities.clear();
    System.out.println("\nAfter invoking the clear() method");
    System.out.println("Cities: "+ cities); // empty ArrayList
    System.out.println("cities size: "+ cities.size());
  }
}

Output:

1
2
3
4
5
6
7
Before invoking the clear() method
Cities: [New York City, Los Angeles, Mountain View, Austin, Atlanta]
cities size: 5

After invoking the clear() method
Cities: []
cities size: 0

Explanation:

  1. I have created an ArrayList object cities
  2. I have added a few String objects to the cities
  3. I have printed the cities to see the String objects present in it. Also, I have printed the size of the cities. We can see its size is 5 since we have added the 5 String objects in it.
  4. Invoke the clear() method on the cities to remove all its elements.
  5. When we print the cities ArrayList again, we can not see any String objects present in it. Also, the size of cities is 0, so we can say the clear() method made it empty.

Conclusion

When we invoke the clear() method on the ArrayList, it removes all the elements from this Arraylist and makes it empty.

1
arrayList.clear()

The example Java program used in the above article can be found at this GitHub repository.

If you know anything else about the clear() method of the ArrayList class, kindly write it in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus