How to Traverse Through ArrayList in Java


The example aims to traverse through an ArrayList object in forward direction with ListIterator’s next and hasNext methods in Java.

Source Code

package com.beginner.examples;

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

public class TraverseForwardUsingListIterator {

	public static void main(String[] args) {
		
		//create ArrayList.
		ArrayList arrayList = new ArrayList();
		
		arrayList.add("A");
		arrayList.add("B");
		arrayList.add("C");
		arrayList.add("D");
		
		ListIterator listIterator = arrayList.listIterator();
		
		while(listIterator.hasNext()) {
			System.out.println(listIterator.next());
		}
	}

}

Output:

A
B
C
D

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments