Home Tutorial Java Iterator Java Next Iterator

 
 

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

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