Sunday, 25 May 2014

LinkedList : pollFirst : Retrieves and Removes First Element

public E pollFirst()
Retrieves and removes the first element of this list. Returns null if this list is empty. Return the first element of this list, or null if this list is empty.

import java.util.*;

class LinkedListPollFirst{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Head Element of myList is ");
  System.out.println(myList.pollFirst());
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 40]
Head Element of myList is 10
Elements in myList are
[20, 30, 40]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment