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)
No comments:
Post a Comment