Home Tutorial Java Iterator Java Hasnext Iterator

 
 

Java Hasnext Iterator
Posted on: November 3, 2009 at 12:00 AM
In this section of tutorial we will learn how to use the HashNext method of the Iterator interface. We will create an example to display the contents of the ArrayList using hashNext

  • Java HasNext Iterator is the method of the Iterator Interface.
  • It returns the boolean.So it is used in the loop to traverse the next value.
  • In case of false vale loop terminates.


Java HasNext Iterator Example
import java.util.ArrayList;
import java.util.Iterator;

public class hasnext {

	public static void main(String[] args) {
		String months[] = { "jan", "feb", "march", "april", "may", "june" };
		ArrayList list = new ArrayList();
		for (String m : months) {
			list.add(m);
		}
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}
}

Output

jan feb march april may june

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