Home Tutorial Java Iterator Iterator Java Sample

 
 

Share on Google+Share on Google+
Iterator Java Sample
Posted on: November 4, 2009 at 12:00 AM
Advertisement
In this section of tutorial we will learn how to create a sample java application using iterator.We will create an example to display the contents of the ArrayList using iterator

  • Java Sample Iterator interface makes the traversing of the elements easy.
  • It works similar to the Enumeration.
  • It has method next() and haswNext()


Java Sample Iterator Example

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class sample {

	public static void main(String[] args) {
		List list = new ArrayList();
		for (int i = 0; i < 5; i++) {
			list.add((float)Math.random() * 100);
		}
		Iterator i = list.iterator();
		while (i.hasNext()) {
			System.out.print(i.next() + "\t");
		}
	}
}

Output :

20.235153 18.181171 13.668783 54.82365 93.62838
Advertisement

Related Tags for Iterator Java Sample:


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