How to clear an ArrayList in Java

By | May 20, 2020

How to clear an ArrayList in Java:

We can use java.util.List.clear() and removeAll() methods to clear elements from an ArrayList.

1. Empty an ArrayList using clear() method:

Syntax:

It removes all of the elements from the list. The list will be empty after this call returns.

Example:

Output:

 

2. Empty an ArrayList using removeAll() method:

Removes from this list all of its elements that are contained in the specified collection. It returns true if this list changed as a result of the call. Otherwise returns false.

Example:

Output:

 

Recommended Posts:

How to remove an element from an ArrayList

How to convert a String to String array in Java

Java interview questions for QA

SQL quick reference for interview preparation

Real-time selenium interview questions

 

 

Tags: How to clear an ArrayList in Java

 

Leave a Reply

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