ArrayList clear() method in Java

The clear() method removes all elements from the ArrayList, or we can say it is used to clear an ArrayList in Java. In this post, we are going to discuss the clear() method of the ArrayList class in detail.

  • Method declaration – public void clear()
  • What does it do? – It will remove all elements from the ArrayList, and the list will empty after calling this method. We can verify the same using the isEmpty() method of the ArrayList.
  • What does it return? – It will return nothing as its return type is void.
Code Example
public class Codekru {

	public static void main(String[] args) {

		ArrayList<String> al = new ArrayList<String>();

		// appending the elements at the end of the list
		al.add("First");
		al.add("Second");

		// printing the linked list
		System.out.println("ArrayList content before: " + al.toString());

		al.clear();

		// printing the linked list
		System.out.println("ArrayList content after: " + al.toString());

	}
}

Output –

ArrayList content before: [First, Second]
ArrayList content after: []
Time Complexity of the ArrayList clear() method

The average time complexity of the clear() method is O(n), where n is the size of the ArrayList. It’s because the clear() method iterates over the ArrayList, marks the elements as null, and changes the size value to 0.

Internal implementation of the ArrayList clear() method
public void clear() {
        modCount++;
        final Object[] es = elementData;
        for (int to = size, i = size = 0; i < to; i++)
            es[i] = null;
}

The What If scenarios

Q – What if we try to use the clear() on a null ArrayList?

It will throw a NullPointerException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		ArrayList<String> al = new ArrayList<String>();

		al = null;

		al.clear();

		// printing the linked list
		System.out.println("ArrayList content after: " + al.toString());

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.clear()" because "al" is null
Q – What if we try to use the clear() method on a list containing null objects only?

It will successfully remove those null objects, as shown in the below program.

public class Codekru {

	public static void main(String[] args) {

		ArrayList<String> al = new ArrayList<String>();

		al.add(null);
		al.add(null);

		// printing the linked list
		System.out.println("ArrayList content before: " + al.toString());

		al.clear();

		// printing the linked list
		System.out.println("ArrayList content after: " + al.toString());

	}
}

Output –

ArrayList content before: [null, null]
ArrayList content after: []

Please visit this link to learn more about the ArrayList class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.