Monday 7 April 2014

addLast : Inserts the element at the end of this deque

void addLast(E e)
Inserts the specified element at the end of this deque.

import java.util.*;

class DequeAddLast{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedList<Integer> ();
  
  System.out.println("Add 10 to the Deque");
  myDeque.addLast(10);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 20 to the Deque");
  myDeque.addLast(20);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 30 to the Deque");
  myDeque.addLast(30);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 40 to the Deque");
  myDeque.addLast(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
[10, 20]

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

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

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 DequeAddLastIllegalState{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  System.out.println("Add 10 to the Deque");
  myDeque.addLast(10);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 20 to the Deque");
  myDeque.addLast(20);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 30 to the Deque");
  myDeque.addLast(30);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 40 to the Deque");
  myDeque.addLast(40);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 50 to the Deque");
  myDeque.addLast(50);
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nAdd 60 to the Deque");
  myDeque.addLast(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
[10, 20]

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

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

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

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

Since Deque capacity is 5, so while trying to add the 6th element 60, IllegalStateException thrown.

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 DequeAddLastNull{
 public static void main(String args[]){
  Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> ();
  
  System.out.println("\nAdd null to the Deque");
  myDeque.addLast(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.linkLast(Unknown Source)
        at java.util.concurrent.ConcurrentLinkedDeque.addLast(Unknown Source)
        at DequeAddLastNull.main(DequeAddLastNull.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