Home Tutorial Java Iterator Java For loop Iterator

 
 

Java For loop Iterator
Posted on: November 3, 2009 at 12:00 AM
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

Related Tags for Java For loop Iterator :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.