Friday, 30 May 2014

ArrayList : iterator : Returns Iterator over List

public Iterator<E> iterator()
Returns an iterator over the elements in this list. This implementation returns a list iterator over the list.

import java.util.*;
class ArrayListIterator1{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  Iterator<Integer> myIter;
  
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  myIter = myList.iterator();
  
  System.out.println("Elements in myList are");
  while(myIter.hasNext()){
   System.out.println(myIter.next());
  }
 }
}

Output
Elements in myList are
10
123
321
456

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment