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

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


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

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

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

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

Syntax

void push(E e) 

This method does not return any value, it only pushes a specified element onto stack represented by the underlying deque.

This method is similar to the addFirst(e) method. In this method element is to be added at the head into deque. This method pushes the specified element at the head unless it is possible to do so instantly without infracting the capacity limitation of deque. If an element is successfully pushed in to the deque this method returns 'true' otherwise, it displays an 'IllegalStateException' when there is no sufficient space is currently available.

Parameter description

e : It takes an element what do you want to push.

Example of push() method

In this example we will show how does push(E e) method work in Deque interface. This example will help you to understand how you can add a specified element at the head into underlying deque.

Example :

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
public class DequePush {
  public static void main(String args[]) {
    Deque dq = new LinkedList();
    dq.add(10);
    dq.add(11);
    dq.add(12);
    System.out.println("Elements of previous deque are :" + dq);
    System.out.println("And the size of deque : " + dq.size());
    System.out.println("Now insert a new element '9' at the front of the deque");
    // This method will add the element at the front of the deque.
    dq.push(9);
    System.out.println("Then the new Elements of deque are : " + dq);
    System.out.println("And the size of new deque = " + dq.size());
  }
}

Output :

Elements of previous deque are :[10, 11, 12]

And the size of deque : 3

Now insert a new element '9' at the front of the deque

Then the new Elements of deque are : [9, 10, 11, 12]

And the size of new deque = 4

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics