Iterate java Arraylist


 

Iterate java Arraylist

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.

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

Ads