Iterator with an example


Using Iterator we can iterate only in forward direction and you cannot add elements while iterating and Here cursor always points to specific index.

Example:
 public class Example {
 
  public static void main(String[] args) {

    ArrayList aList = new ArrayList();
 
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
 
    Iterator itr = aList.iterator();
 
     while(itr.hasNext())
      System.out.println(itr.next());
 
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.