Home > Java > javaTutorial > body text

How to delete all elements of ArrayList in Java?

PHPz
Release: 2023-09-20 14:21:04
forward
1019 people have browsed it

How to delete all elements of ArrayList in Java?

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()
Copy after login

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)
Copy after login

Removes from this list all elements contained in the specified collection.

Parameters

  • c< /strong> - The collection containing the elements to be removed from this list.

Returns

Returns True if this list changed as a result of the call

Throws

  • ##UnsupportedOperationException - if this list does not support the removeAll operation.

  • 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.

Example 1< h2>The following is an example showing the usage of clear() 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.clear();
      System.out.println("Cleared List: " + list);
   }
}
Copy after login

Output

This will produce the following results -

List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Cleared List: []
Copy after login
Copy after login

Example 2

The 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);
   }
}
Copy after login

Output

This will produce the following results-

List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Cleared List: []
Copy after login
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template