Home Tutorial Java Iterator Iterate java Arraylist

 
 

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

Related Tags for Iterate java Arraylist:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.