Java Reference Library →

ArrayList clear() method - Syntax and Examples

ArrayList clear() method - Syntax and Examples

Learn about the Java ArrayList clear() method. This tutorial covers its syntax, parameters, return value, and provides practical examples.

This reference explains ArrayList clear() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

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.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →