Home Tutorial Java Iterator Java List Iterator

 
 

Java List Iterator
Posted on: November 3, 2009 at 12:00 AM
In this tutorial we will see about the iterator interface and its functionality.We will create an example for the Iterator

  • Java List Iterator is an interface in the collection framework
  • It traverses through all the elements of the collection.
  • It works like enumeration.
  • It has methods hasNext() and next().


Example of Java List Iterator

import java.util.*;

public class iterator {

	public static void main(String[] args) {
		List list = new ArrayList();
		for (int i = 0; i < 5; i++) {
			list.add(new Random().nextInt(100));
		}
		Iterator i = list.iterator();
		while (i.hasNext()) {
			System.out.print(i.next() + "\t");
		}
	}
}

Output

39 73 87 49 1

Related Tags for Java List 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.