Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList clear() Method

Posted on July 22, 2025July 22, 2025 By Admin No Comments on Java ArrayList clear() Method

The clear() method is a fundamental operation available in the Java ArrayList class. It allows you to remove all elements from an ArrayList with a single command, resetting the list to an empty state while maintaining its original capacity. This makes it an essential tool for managing collections that need to be reused or reset during a program’s execution.

What Is the clear() Method in ArrayList?

  • The clear() method removes all elements from the list.
  • After calling clear(), the size of the list becomes zero, but the underlying array capacity is not decreased unless trimToSize() is explicitly called.
  • The method does not return a value (its return type is void).

Syntax

public void clear()

Parameters

  • The method does not accept any parameters.

Return Value

  • void – There is no return value. The ArrayList is simply emptied.

Exceptions

  • The clear() method may throw an UnsupportedOperationException if the list does not support the clear operation (this is rare and only for custom, immutable list implementations).
  • For standard ArrayList, there are no typical exceptions.

How Does clear() Work Internally?

  • Internally, clear() sets the size of the ArrayList to zero and dereferences all elements to allow for garbage collection.
  • The capacity of the underlying array does not change; it will only shrink if you call trimToSize() after clearing.

Examples of the clear() Method

1. Clear All Elements

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");

        System.out.println("Before clear: " + colors); // [Red, Green, Blue]
        colors.clear();
        System.out.println("After clear: " + colors); // []
    }
}

Output:

Before clear: [Red, Green, Blue]
After clear: []

2. Using clear() to Reuse the Same List

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);

        System.out.println("First version: " + numbers); // [10, 20]
        numbers.clear();

        numbers.add(30);
        numbers.add(40);
        System.out.println("Reused list: " + numbers); // [30, 40]
    }
}

Output:

First version: [10, 20]
Reused list: [30, 40]

Important Notes

  • The clear() method is useful for resetting lists that are being reused in loops or throughout the application.
  • The internal array capacity remains the same after clear(), so adding new items after clearing does not immediately require reallocation.
  • No elements are individually removed; references are cleared for faster performance and better memory cleanup.
  • If the list is already empty, calling clear() has no effect or side-effects.

Summary

AspectDetails
Method PurposeRemove all elements from an ArrayList
Syntaxlist.clear()
Return Valuevoid (no value returned)
ParametersNone
Throws ExceptionRarely, UnsupportedOperationException
Typical Use CasesResetting, emptying, or reusing lists

Typical Use Cases

  • Clearing user input lists after processing form data.
  • Initializing or reinitializing a list in batch operations.
  • Managing memory by releasing object references before building fresh data in the same list.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList addAll() Method
Next Post: Java ArrayList clone() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method

Recent Posts

  • Java ArrayList remove() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList iterator() Method
  • Java ArrayList isEmpty() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme