size ( ) method of Deque Interface in java.

size ( ) method of Deque Interface in java.


Posted in : Core Java Posted on : February 5, 2011 at 7:01 PM Comments : [ 0 ]

int size ( ) This method returns the total number of elements of the underlying deque.

size ( ) method of Deque Interface in java.

In this section we will discuss how can size ( ) method be implemented in Deque interface in java.

Syntax

int size ( )

This method returns the total number of elements of the underlying deque.

Using this method you can find the number of elements of an existing deque. If deque contains more than Integer.MAX_VALUE elements, it returns the same.

Parameter Description 

It does not take any argument, only returns integer (number of elements of a deque) value.

Example of size( ) method 

In this example we will show you how does size( ) method work in Deque interface. In the following example at first we will insert elements by an add() method then after check their existance into the deque using isEmpty() method, and finally we will implement the size() method that returns the final size of deque. 

Example:-

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
public class DequeSize {
public static void main (String args []){
  int size;
  Deque dq=new LinkedList();
  dq.add(1);
  dq.add(2);
  dq.add(3);
  dq.add(4);
  dq.add(5);
  dq.add(6);
  System.out.println("Is deque empty ? "+dq.isEmpty());
  if (dq != null)
  {
    System.out.println("Deque is  : "+dq);
  }
  else
    System.out.println("Deque have no elements " +dq);
   size= dq.size();
    System.out.println("Size of deque : "+size);
  //When remove the elements from list
  dq.clear();
    System.out.println("Is Deque empty ? "+dq.isEmpty());
    System.out.println("Deque have no elements " +dq);
    System.out.println("Size of deque : " +dq.size());
  }
}

Output :

Is deque empty ? false

Deque is : [1, 2, 3, 4, 5, 6]

Size of deque : 6

Is Deque empty ? true

Deque have no elements []

Size of deque : 0

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics