The List interface extends the Collection interface and stores a sequence of elements. The List interface provides two methods to efficiently insert and delete multiple elements at any point in the list. Unlike sets, lists allow duplicate elements and multiple null values if null values are allowed in the list. List provides add and remove methods to add/remove elements. In order to clear the list or remove all elements from the list, we can use the clear() method of List. We can also use the removeAll() method to achieve the same effect as the clear() method.
In this article, we will introduce the clear() and removeAll() methods with corresponding examples.
Syntax - clear() method
void clear()
Comments
Removes all elements from this list.
The list will be empty after this call returns.
Throws
UnsupportedOperationException - if this list does not support clear operations.
Syntax -removeAll() method
boolean removeAll(Collection<?> c)
Removes from this list all elements contained in the specified collection.
Parameters
c - The collection containing the elements to be removed from this list.
Returns
Returns True if this list changed as a result of the call p>
Throws
-
##UnsupportedOperationException - if this list does not support the removeAll operation.
li> ClassCastException - if the class of an element in this list is incompatible with the specified collection (optional).
-
NullPointerException - If this list contains null elements and the specified collection does not allow null elements (optional), or the specified collection is null.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9)); System.out.println("List: " + list); list.clear(); System.out.println("Cleared List: " + list); } }OutputThis will produce the following results -
List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Cleared List: []Example 2The following example shows the usage of removeAll() method -
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9)); System.out.println("List: " + list); list.removeAll(list); System.out.println("Cleared List: " + list); } }OutputThis will produce the following results-
List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Cleared List: []
The above is the detailed content of How to delete all elements of ArrayList in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor