Pin It

How to use Iterator Interface to iterate/traverse Java Collections ?.

Program to demonstrate Iterator Interface to traverse a Java Collection

package com.hubberspot.collections.example;

import java.util.*;

public class IteratorExample
{
 public static void main(String[] args)
 {
  ArrayList list = new ArrayList();
  list.add("One");
  list.add("Two");
  list.add("Three");
  list.add("Four");
  
  Iterator iterate = list.iterator();
  while(iterate.hasNext())
  {
      String element = (String)iterate.next();
      System.out.println("Element = " + element);
         
      iterate.remove();  
 } 
 
   System.out.println("Array list contains"
      + list);
}
}


Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot