add(E e) method of Deque Interface in Java.

add(E e) method of Deque Interface in Java.


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

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

add(E e) method of Deque Interface in Java.

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

Syntax

boolean add(E e) 

This method adds the element at the end of the queue, represented by 'deque'.

It adds an element to the underlying deque if it is possible to do so instantly without breaking the capacity limitation of deque. In such case if space is not  available presently in deque, this method throws an 'IllegalStateException' exception otherwise, returns 'true' upon successful addition of element into deque.

Parameter description

e : It takes an element what do you want to insert into deque.

Example of add(E e) method

This example will help you to understand how a specified element can be added at the end into deque and how it works In the following example we will implement the add(e) method to insert an element at the end into deque then after find the total number of elements of  the underlying deque using size() method before and after adding elements. To check whether add(e) method is truly added the element at last into the deque we will again use this method into the middle of this program.

Example : 

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
public class DequeAddElement {
  public static void main (String args[]){
    Deque dq = new LinkedList();
    System.out.println("Initially the size of dqueue is " + dq.size());
    System.out.println("Elements in dqueue : "+dq);
    dq.add(11);
    dq.add(12);
    System.out.println("After addition of elements into dqueue, the elements are :"+dq);
    System.out.println("And the size of dqueue : " + dq.size());
    System.out.println("Now insert a new element '13'");
    dq.add(13);
    System.out.println("Then the new Elements of dqueue are : " + dq);
    System.out.println("And the size of new dqueue = " + dq.size());
  }
}

Output :

Initially the size of dqueue is 0

Elements in dqueue : []

After addition of elements into dqueue, the elements are :[11, 12]

And the size of dqueue : 2

Now insert a new element '13'

Then the new Elements of dqueue are : [11, 12, 13]

And the size of new dqueue = 3 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics