Collections

Arraylist clear() in Java Code With Examples

Table of Contents

Program

import java.util.ArrayList;
public class ArrayListClear
{
    public static void main(String[] args)
    {
        ArrayList<Integer> arr = new ArrayList<Integer>(4);
        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);
        System.out.println("The list initially: " + arr);
        arr.clear();
        System.out.println("The list after using clear() method: " + arr);
    }
}

Output

The list initially: [1, 2, 3, 4]
The list after using clear() method: []

Description

public void clear()

Removes all of the elements from this list. The list will be empty after this call returns.

Specified by:

clear in interface Collection<E>
clear in interface List<E>

Overrides:

clear in class AbstractList<E>