Share on Google+Share on Google+

Iterate java Arraylist

Ads
In this tutorial we will see how to use the iterator interface with ArrayList. We will create an example to display the contents of the ArrayList.

  • Iterator is an interface in the collection framework.
  • ArrayList is a collection class and implements the List Inteface.
  • All the elements of the ArrayList can be traversed by the Iterator.
  • Iterator has methods hasNext() and next().


Example of Java Arraylist Iterate

import java.util.*;

public class javaarray {
	public static void main(String[] args) {

		List list = new ArrayList();
		list.add("aaaa");
		list.add("bbbb");
		list.add("cccc");
		list.add("dddd");
		Iterator i = list.iterator();
		while (i.hasNext()) {
			System.out.print(i.next() + "\t");
		}
	}
}

Output

aaaa bbbb cccc dddd

Advertisements

Posted on: November 2, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.