Saturday 12 April 2014

element : Retrieves the head of the queue

E element()
Retrieves, but does not remove, the head of the queue. This method is equivalent to getFirst(). This method is equivalent to getFirst().

import java.util.*;

class DequeElement{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedList<Integer> ();
  
    /* Add Elements to myDeque */
    myDeque.add(10);
    myDeque.add(20);
    myDeque.add(30);
    myDeque.add(40);
  
    System.out.println("Elements in Deque are " + myDeque);
  
    System.out.println("\nHead Of the Deque are " + myDeque.element());
    System.out.println("\nHead Of the Deque are " + myDeque.element());
  }
}

Output
Elements in Deque are [10, 20, 30, 40]

Head Of the Deque are 10

Head Of the Deque are 10

1. throws NoSuchElementException if this deque is empty
import java.util.*;

class DequeElementNoSuch{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedList<Integer> ();
  
    System.out.println("Elements in Deque are " + myDeque); 
    System.out.println("\nHead Of the Deque are " + myDeque.element());

  }
}

Output
Elements in Deque are []
Exception in thread "main" java.util.NoSuchElementException
        at java.util.LinkedList.getFirst(LinkedList.java:242)
        at java.util.LinkedList.element(LinkedList.java:661)
        at DequeElementNoSuch.main(DequeElementNoSuch.java:8)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment