Java ArrayList clear() Method Tutorial

In this section, we will learn what the ArrayList clear() method is and how to use it in Java.

What is Java ArrayList clear() Method?

The Java ArryaList clear() method is used to remove the elements of an ArrayList object.

Basically, calling this method will result in removing the entire elements of an ArrayList and after that, the size of that ArrayList object becomes 0 because there’s no element in it anymore.

Java ArrayList clear() Method Syntax:

public void clear()

clear () Method Parameters:

This method does not take an argument.

clear () Method Return Value:

The return value of this method is void.

clear () Method Exceptions:

The method throws the `UnsupportedOperationException` if the target list object does not support this method.

Example: using ArrayList clear () method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        System.out.println("The size of the list before calling the clear method: "+list.size());
        System.out.println("The size of the list is: "+list.size());


The size of the list before calling the clear method: 3

The size of the list is: 0

As you can see, calling the clear() method would remove the entire elements of a list object.

Top Technologies