In Java, clear() removes all elements from an ArrayList. Afterwards size() is 0 and isEmpty() is true. The method returns void.
The list object itself stays the same, so every variable that refers to it now sees an empty list, and you can keep adding to it. In current JDKs clear() does not shrink the internal array (an implementation detail); call trimToSize() afterwards if you need that memory back.
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.
Syntax
Call clear with no arguments.
Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b"));
list.clear(); // []
list.add("c"); // [c], same list object
Call
What it does
list.clear()
Removes every element.
list = new ArrayList<>()
Points this one variable at a new list; others still see the old one.
list.subList(from, to).clear()
Removes only a range of indexes.
list.removeIf(x -> true)
Also empties the list, but less clearly.
clear() sets every used slot of the internal array to null so the old elements can be garbage collected. Its running time grows with the number of elements.
Library declarations
Java
// java.util.ArrayList<E>
public void clear()
// Declared in Collection<E> and List<E>
Parameters
Parameter
Description
None
clear() takes no parameters.
Return Value
Return Value
Description
void
Nothing is returned. The list is empty afterwards.
Full basket. The list starts with three items, so its size is 3.
One call empties it.clear() removes all three; the list prints as [] with size 0.
Same object. The variable still points to a working ArrayList, so add("bread") just works.
Ready for reuse. No new list had to be created to start over.
Example 2 — clear() Affects Every Reference
An alias and a copy react differently when the original list is cleared.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListClearShared {
public static void main(String[] args) {
List<Integer> data = new ArrayList<>(Arrays.asList(1, 2, 3));
List<Integer> sameList = data;
List<Integer> copy = new ArrayList<>(data);
data.clear();
System.out.println("data: " + data);
System.out.println("sameList: " + sameList);
System.out.println("copy: " + copy);
}
}
Output
data: []
sameList: []
copy: [1, 2, 3]
How It Works
Alias vs copy.sameList is just another name for data; copy is a separate ArrayList with its own elements.
Clear through one name.data.clear() empties the shared list object.
The alias sees it.sameList prints [], because it points to the very same list.
The copy is safe.copy was filled before clear() and keeps [1, 2, 3].
Example 3 — Clear Only Part of a List with subList()
Remove the middle letters through a view, then clear the rest the same way.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListClearRange {
public static void main(String[] args) {
List<Character> letters = new ArrayList<>(Arrays.asList('a', 'b', 'c', 'd', 'e', 'f'));
letters.subList(1, 4).clear();
System.out.println("after subList(1, 4).clear(): " + letters);
letters.subList(0, letters.size()).clear();
System.out.println("after full range: " + letters);
}
}
Output
after subList(1, 4).clear(): [a, e, f]
after full range: []
How It Works
A view, not a copy.subList(1, 4) covers indexes 1, 2, and 3: b, c, and d.
Clearing the view.clear() on that view removes those three letters from the real list.
What is left. a stays at index 0, and e and f slide down to indexes 1 and 2.
Whole range.subList(0, size()).clear() empties the list, the same as calling clear() directly.
Frequently Asked Questions
It removes every element, leaving an empty list that you can keep using.
clear() empties the existing object, so every reference to it sees an empty list. Assigning a new ArrayList only changes the one variable; other references still see the old elements.
Not in current JDKs; the internal array keeps its length. Call trimToSize() afterwards to release the unused space.
Use clear() to remove everything. removeAll(c) is for removing only the elements that appear in another collection.