E
getFirst()
Retrieves,
but does not remove, the first element of this deque.
import java.util.*; class DequeGetFirst{ 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 First Element Of the Deque " + myDeque.getFirst()); System.out.println("Elements in the myDeque are " +myDeque); System.out.println("\nRetrieving the First Element Of the Deque " + myDeque.getFirst()); System.out.println("Elements in the myDeque are " +myDeque); } }
Output
Elements in the myDeque are [10, 20, 30, 40] Retrieving the First Element Of the Deque 10 Elements in the myDeque are [10, 20, 30, 40] Retrieving the First Element Of the Deque 10 Elements in the myDeque are [10, 20, 30, 40]
1. throws
NoSuchElementException if this deque is empty
import java.util.*; class DequeGetFirstNoSuch{ 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 First Element Of the Deque " + myDeque.getFirst()); 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.getFirst(LinkedList.java:242) at DequeGetFirstNull.main(DequeGetFirstNull.java:12)
No comments:
Post a Comment