Home Tutorial Java Iterator Java Hasnext Iterator

 
 

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

Related Tags for Java Hasnext 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 3, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com