Home Tutorial Java Iterator Iterator Java While

 
 

Share on Google+Share on Google+
Iterator Java While
Posted on: November 4, 2009 at 12:00 AM
Advertisement
This segment of tutorial illustrates about the while loop and its use with the Iterator interface.We will create an example to print the content of the a ArrayList using the while loop.

  • The Java While loop Iterator is the top tested loop.
  • It is in two forms while(), do while()
  • While loop is mostly used with Iterator compared to for loop


Java While Loop Iterator Example
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class while1 {
	public static void main(String[] args) {
		List l = new ArrayList();
		for (int i = 10; i <= 50; i += 10) {
			l.add(i);
		}
		Iterator it = l.iterator();
		System.out.println("while loop");
		while (it.hasNext()) {
			System.out.print(it.next() + "\t");
		}
		Iterator it1 = l.iterator();
		System.out.println("\ndo .. while");

		do {
			System.out.print(it1.next() + "\t");
		} while (it1.hasNext());
	}
}
Advertisement

Related Tags for Iterator Java While :


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