Share on Google+Share on Google+

Java For loop Iterator

Advertisement
In this tutorial we will learn how to use the Iterator interface as Array. We will create an example print the multiple ArrayList at a time using the Iterator.

  • Iterator can be used with the for loop also.
  • Initialization of the Iterator is done before the for loop.
  • Iterator has boolean value with the hasNext method.
  • So no increment/decrement is required.


Java for Loop Iterator Example
import java.util.ArrayList;
import java.util.Iterator;

public class javafor {
	public static void main(String[] args) {
		ArrayList list = new ArrayList();
		list.add(new Integer(111));
		list.add(222);
		list.add(new Integer(333));
		list.add(444);

		Iterator it = list.iterator();
		for (; it.hasNext();) {
			System.out.println(it.next());
		}
	}
}

Output

111 222 333 444

Advertisements

Posted on: November 3, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.