Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ArrayList clear() method
Syntax and Examples


Introduction

The clear() method in Java's `ArrayList` class is a straightforward way to remove all elements from the list. Think of it like emptying a container—all its contents are gone.

Syntax


public void clear()

Parameters

Parameter Description
None This method does not accept any parameters.

Return Value

The clear() method doesn't return anything (void). It modifies the original list directly.

Examples

Example 1: Clearing an ArrayList with Initial Elements

This example demonstrates how to clear an `ArrayList` that already contains elements. We’ll create a list, add some values, and then use clear() to remove them all.


import java.util.ArrayList;

public class ClearExample1 {
    public static void main(String[] args) {
        // Create an ArrayList of strings
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        System.out.println("Before clear(): " + names);

        // Clear the ArrayList
        names.clear();

        System.out.println("After clear(): " + names);
    }
}


Before clear(): [Alice, Bob, Charlie]
After clear(): []

Explanation: We initialized an ArrayList called names with three strings. The first print statement shows the contents before clearing. Calling names.clear() removes all elements. The second print statement confirms that the list is now empty.

Example 2: Clearing an Empty ArrayList

This example shows what happens when you attempt to clear an `ArrayList` that’s already empty. It won't cause any errors; it simply has no effect.


import java.util.ArrayList;

public class ClearExample2 {
    public static void main(String[] args) {
        // Create an empty ArrayList of integers
        ArrayList<Integer> numbers = new ArrayList<>();

        System.out.println("Before clear(): " + numbers);

        // Clear the ArrayList (no effect since it's already empty)
        numbers.clear();

        System.out.println("After clear(): " + numbers);
    }
}


Before clear(): []
After clear(): []

Explanation: We created an empty ArrayList called numbers. The first print statement shows that the list is indeed empty. Calling numbers.clear() does nothing because there are no elements to remove.

Example 3: Clearing and Adding Elements Again

This example demonstrates clearing an ArrayList, then adding new elements afterward to show how the list state changes completely.


import java.util.ArrayList;

public class ClearExample3 {
    public static void main(String[] args) {
        // Create an ArrayList of doubles
        ArrayList<Double> values = new ArrayList<>();
        values.add(1.5);
        values.add(2.7);
        values.add(3.9);

        System.out.println("Before clear(): " + values);

        // Clear the ArrayList
        values.clear();

        System.out.println("After clear(): " + values);

        // Add new elements
        values.add(4.2);
        values.add(5.8);

        System.out.println("After adding new elements: " + values);
    }
}


Before clear(): [1.5, 2.7, 3.9]
After clear(): []
After adding new elements: [4.2, 5.8]

Explanation: We initialized an ArrayList called values with three double values. We cleared the list and then added two more double values. The output shows that after clearing, the list was empty, and subsequently contained just the newly added elements.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M