w3resource

Java ArrayDeque Class: size() Method

public int size()

The size() method is used to count the number of elements in a given deque.

Package: java.util

Java Platform: Java SE 8

Syntax:

size()

Return Value:

the number of elements in this deque

Return Value Type: int

Pictorial Presentation

Java ArrayDeque Class: size() Method

Example: Java ArrayDeque Class: size() Method

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

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

      // Use add() method to add elements in the deque
      deque.add(100);
      deque.add(200);
      deque.add(150);
      deque.add(95);  
      deque.add(200); 

      // Print all the elements of the original deque
      System.out.println("Elements of the original deque:");
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

     // size() will print the size of this deque
      int deque_size  = deque.size();
      System.out.println("Size of deque = " + deque_size); 
   }
}

Output:

Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Number = 200
Size of deque = 5

Java Code Editor:

Previous:removeLastOccurrence Method
Next:Currency Home



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://w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_size.php