Home Tutorial Java Iterator Iterate java Arraylist

 
 

Share on Google+Share on Google+
Iterate java Arraylist
Posted on: November 2, 2009 at 12:00 AM
Advertisement
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
Advertisement

Related Tags for Iterate java Arraylist:


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 2, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com