A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_clear.php below:

Website Navigation


Java ArrayDeque Class: clear() Method

Java ArrayDeque Class: clear() MethodLast update on August 19 2022 21:50:42 (UTC/GMT +8 hours) 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

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




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4