w3resource

Java ArrayDeque Class: clear() Method

public void clear()

The clear() method is used to removes all of the elements from a given deque.

Package: java.util

Java Platform: Java SE 8

Syntax: clear()

Return Value Type: void

Pictorial Presentation

Java ArrayDeque Class: clear() Method

Example: Java ArrayDeque Class: clear() Method

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
   public static void main(String[] args) {
      
      // create an empty array deque with an initial capacity
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(10);
      deque.add(20);
      deque.add(30);

      // print all the elements available in deque
      for (Integer n : deque) {
         System.out.println("Number = " + n);
      }      

      // Let clear all the elements in the deque
      deque.clear();
 
      // print all the elements available in deque
      System.out.println("After clearing all the elements in the deque:");
      
      for (Integer n : deque) {
         System.out.println("Number = " + n);
      }          
     }
}   

Output:

Number = 10
Number = 20
Number = 30
After clearing all the elements in the deque:

Java Code Editor:

Previous: addLast Method
Next: clone Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_clear.php