Friday 11 April 2014

getLast : Retrieves the last element of this deque

E getLast()
Retrieves, but does not remove, the last element of this deque.

import java.util.*;

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

Output
Elements in the myDeque are [10, 20, 30, 40]

Retrieving the Last Element Of the Deque 40
Elements in the myDeque are [10, 20, 30, 40]

Retrieving the Last Element Of the Deque 40
Elements in the myDeque are [10, 20, 30, 40]

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

class DequeGetLastNoSuch{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedList<Integer> ();

  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRetrieving the Last Element Of the Deque " + myDeque.getLast());
  System.out.println("Elements in the myDeque are " +myDeque); 
 }
}

Output
Elements in the myDeque are []
Exception in thread "main" java.util.NoSuchElementException
        at java.util.LinkedList.getLast(LinkedList.java:255)
        at DequeGetLastNoSuch.main(DequeGetLastNoSuch.java:9)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment