How to Iterate Through Java Arraylist


In this example we will show how to iterate through the elements of java ArrayList object with Iterator.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.Iterator;

public class UseIteratorArraylistExample {

	
	public static void main(String[] args) {
		
		ArrayList al = new ArrayList();
		
		al.add("A");
		al.add("B");
		al.add("C");
		al.add("D");
		al.add("E");
		
		//Get Iterator 
		Iterator iterator = al.iterator();
		while(iterator.hasNext())
		{
			String element = iterator.next();
			System.out.println(element);
		}

	}

}

Output:

A
B
C
D
E

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments