Home Tutorial Java Iterator Java Next Iterator

 
 

Share on Google+Share on Google+
Java Next Iterator
Posted on: November 4, 2009 at 12:00 AM
Advertisement
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
Advertisement

Related Tags for Java Next Iterator :


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.

Posted on: November 4, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com