Clear ArrayList in Java Example

This Java example shows how to clear ArrayList in Java using a clear() method with an example.

Clear ArrayList in Java Example

package com.javaguides.collections.arraylistexamples;

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

public class RemoveElementsFromArrayListExample {
    public static void main(String[] args) {
        List < String > programmingLanguages = new ArrayList < > ();
        programmingLanguages.add("C");
        programmingLanguages.add("C++");
        programmingLanguages.add("Java");
        programmingLanguages.add("Kotlin");
        programmingLanguages.add("Python");
        programmingLanguages.add("Perl");
        programmingLanguages.add("Ruby");

        System.out.println("Initial List: " + programmingLanguages);

        // Remove all elements from the ArrayList
        programmingLanguages.clear();
        System.out.println("After clear(): " + programmingLanguages);
    }
}

Output

Initial List: [C, C++, Java, Kotlin, Python, Perl, Ruby]
After clear(): []





Comments