Tuesday 8 April 2014

offerLast : Inserts the element at the end of the deque

boolean offerLast(E e)
Inserts the specified element at the end of this deque. return true if the element was added to this deque, else false.

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

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

Output
Is 10 added to the Deque true
Elements in Deque are
[10]

Is 20 added to the Deque true
Elements in Deque are
[10, 20]

Is 30 added to the Deque true
Elements in Deque are
[10, 20, 30]

Is 40 added to the Deque true
Elements in Deque are
[10, 20, 30, 40]

Is 50 added to the Deque true
Elements in Deque are
[10, 20, 30, 40, 50]

Is 60 added to the Deque false
Elements in Deque are
[10, 20, 30, 40, 50]

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

2. 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 DequeOfferLastNull{
 public static void main(String args[]){
  Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> ();
  
  System.out.println("\nAdd null to the Deque");
  myDeque.offerLast(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.offerLast(Unknown Source)
        at DequeOfferLastNull.main(DequeofferLastNull.java:9)

3. 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