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

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


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

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

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

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

Syntax

void addLast(E e) 

This method adds the element at the last position into deque.

This method is equivalent to the add(E e) method. In this method you can insert a specified element at the end position of the underlying deque. It inserts an element to the underlying deque if it is possible to do so instantly without breaking the capacity limitation of deque. This method returns no value but, it displays an 'IllegalStateException' exception when adding operation gets failed due to capacity restriction of deque.

Parameter description

e : It takes an element what do you want to add in deque.

Example of addLast(E e) method

In this example we will show you how does addLast(E e) method work in Deque interface in java. In the following example we will show how you can add a specified element at the end position of deque, and count the total number of elements of deque before and after adding the elements.

 Example :

package devmanuals.com;
import java.util.Deque;
import java.util.ArrayDeque;
public class DequeAddLast {
  public static void main(String args[]) {
    Deque dqa = new ArrayDeque();
    dqa.add(11);
    dqa.add(12);
    dqa.add(13);
    dqa.add(14);
    System.out.println("Elements of previous deque are :" + dqa);
    System.out.println("And the size of deque : " + dqa.size());
    System.out.println("Insert a new element '10' at the front of the deque");
    // This method will add the element at the front of the deque.
    dqa.addFirst(10);
    System.out.println("Then the new Elements of deque are : " + dqa);
    System.out.println("Now insert a new element '15' at the end of the deque");
    // This method will add the element at the end position of the deque.
    dqa.addLast(15);
    System.out.println("Then the new Elements of deque are : " + dqa);
    System.out.println("And the size of new deque = " + dqa.size());
  }
}

Output :

Elements of previous deque are :[11, 12, 13, 14]

And the size of deque : 4

Insert a new element '10' at the front of the deque

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

Now insert a new element '15' at the end of the deque

Then the new Elements of deque are : [10, 11, 12, 13, 14, 15]

And the size of new deque = 6 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics