Java Utility Library

Java ArrayDeque - clear() Method



The java.util.ArrayDeque.clear() method is used to clear all elements of the deque. This method makes the deque empty with a size of zero.

Syntax

public void clear()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.ArrayDeque.clear() method is used to clear all elements of the deque called MyDeque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);

    //printing ArrayDeque
    System.out.println("Before applying clear() method.");
    System.out.println("MyDeque contains: " + MyDeque);

    MyDeque.clear();

    //printing ArrayDeque
    System.out.println("\nAfter applying clear() method."); 
    System.out.println("MyDeque contains: " + MyDeque);   
  }
}

The output of the above code will be:

Before applying clear() method.
MyDeque contains: [10, 20, 30]

After applying clear() method.
MyDeque contains: []

❮ Java.util - ArrayDeque