How to Iterate Through an ArrayList in Java


Through this example, we may simply learn how to iterate through the elements of java ArrayList object in forward and backward direction with ListIterator.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.ListIterator;

public class UseListIterateExample {

	
	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");
		
		//Gets an iterator specific to the List collection
		//This iterator has more methods to operate on the List collection
		ListIterator iterator = al.listIterator();
		
		while(iterator.hasNext())
		{
			//Gets the next element
			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