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[]) { Dequequeue = 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] |
[ 0 ] Comments