Add Element To First and Last Position Of Java Deque Example

Add Element To First and Last Position Of Java Deque Example


Posted in : Java Posted on : December 1, 2010 at 4:20 PM Comments : [ 0 ]

This Java Example shows how to add an element at first and last position in java Deque object.

Add Element To First and Last Position Of Java Deque Example

This Java Example shows how to add an element at first and last  position in java Deque object. To add an element at first and last position of Deque We use void addFirst(Object obj) And addLast(Object obj) method. These method inserts the specified element at the First and last position in the Deque respectively. 

 Example: AddFirstAndAddLastElementDeque.java

package devmanuals.com;

import java.util.*;

public class AddFirstAndAddLastElementDeque {

	public static void main(String args[]) {

		Deque queue = new ArrayDeque();

		queue.add("A");
		queue.add("B");
		queue.add("C");
		queue.add("D");
		System.out.println("The Deque elements before addition are : " + queue);
		queue.addFirst("1");
		queue.addLast("5");
		System.out.println("The Deque elements after addition are : " + queue);
	}
}

Output:

The Deque elements before addition are : [A, B, C, D] 

The Deque elements after addition are : [1, A, B, C, D, 5]

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics