Monday 7 April 2014

addFirst : Inserts the element at the front

void addFirst(E e)
Inserts the specified element at the front of this deque.

import java.util.*;

class DequeAddFirst{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedList<Integer> ();
  
  System.out.println("Add 10 to the Deque");
  myDeque.addFirst(10);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 20 to the Deque");
  myDeque.addFirst(20);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 30 to the Deque");
  myDeque.addFirst(30);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 40 to the Deque");
  myDeque.addFirst(40);
  System.out.println("Elements in Deque are\n" + myDeque);
 }
}

Output
Add 10 to the Deque
Elements in Deque are
[10]

Add 20 to the Deque
Elements in Deque are
[20, 10]

Add 30 to the Deque
Elements in Deque are
[30, 20, 10]

Add 40 to the Deque
Elements in Deque are
[40, 30, 20, 10]

1. throws IllegalStateException if the element cannot be added at this
time due to capacity restrictions
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequeAddIllegalState{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  System.out.println("Add 10 to the Deque");
  myDeque.addFirst(10);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 20 to the Deque");
  myDeque.addFirst(20);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 30 to the Deque");
  myDeque.addFirst(30);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 40 to the Deque");
  myDeque.addFirst(40);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 50 to the Deque");
  myDeque.addFirst(50);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 60 to the Deque");
  myDeque.addFirst(60);
  System.out.println("Elements in Deque are\n" + myDeque);
 }
}

Output
Add 10 to the Deque
Elements in Deque are
[10]

Add 20 to the Deque
Elements in Deque are
[20, 10]

Add 30 to the Deque
Elements in Deque are
[30, 20, 10]

Add 40 to the Deque
Elements in Deque are
[40, 30, 20, 10]

Add 50 to the Deque
Elements in Deque are
[50, 40, 30, 20, 10]

Add 60 to the Deque
Exception in thread "main" java.lang.IllegalStateException: Deque full
        at java.util.concurrent.LinkedBlockingDeque.addFirst(Unknown Source)
        at DequeAddIllegalState.main(DequeAddIllegalState.java:29)

As You Observe the program, Deque has the size 5, so while trying to add the 6th element 60, addFirst() throws IllegalStateException.

2. throws ClassCastException if the class of the specified element
prevents it from being added to this deque

3. throws NullPointerException if the specified element is null and this
deque does not permit null elements
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;

class DequeAddFirstNull{
 public static void main(String args[]){
  Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> ();
  
  System.out.println("\nAdd null to the Deque");
  myDeque.addFirst(null);
  System.out.println("Elements in Deque are\n" + myDeque);
 }
}

Output
Add null to the Deque
Exception in thread "main" java.lang.NullPointerException
        at java.util.concurrent.ConcurrentLinkedDeque.checkNotNull(Unknown Source)
        at java.util.concurrent.ConcurrentLinkedDeque.linkFirst(Unknown Source)
        at java.util.concurrent.ConcurrentLinkedDeque.addFirst(Unknown Source)
        at DequeAddFirstNull.main(DequeAddFirstNull.java:9)

4. throws IllegalArgumentException if some property of the specified
element prevents it from being added to this deque








Prevoius                                                 Next                                                 Home

No comments:

Post a Comment