Share on Google+Share on Google+

Java Next Iterator

Ads
This section of Java tutorial demonstrates how to use next method of the Iterator Interface. We will create an example to display the contents of the map with Iterator.

  • Iterator is used by the set, List Interface and its subclasses.
  • Iterator has next method to get the next element.
  • Next method returns the object.
  • So it should be casted for further use.


Example of Java Next Iterator
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class next {
	public static void main(String[] args) {
		Integer ar[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };
		ArrayList list = new ArrayList();
		for (Integer int1 : ar) {
			list.add(int1);
		}

		Iterator it = list.iterator();
		while (it.hasNext()) {
			Integer myint = (Integer) it.next();
			myint = myint + 9;
			System.out.print(myint + "\t");
		}
	}
}

Output

20 31 42 53 64 75 86 97 108

Advertisements

Posted on: November 4, 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.

Advertisement