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 unlesstrimToSize()
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 anUnsupportedOperationException
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 theArrayList
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
Aspect | Details |
---|---|
Method Purpose | Remove all elements from an ArrayList |
Syntax | list.clear() |
Return Value | void (no value returned) |
Parameters | None |
Throws Exception | Rarely, UnsupportedOperationException |
Typical Use Cases | Resetting, 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.