How to Traverse ArrayList using Iterator in Java
In this Java core tutorial we learn how to traverse through an java.util.ArrayList using java.util.Iterator interface in Java programming language.
How to traverse through an ArrayList using Iterator in Java
In Java, with a given ArrayList object we can use the List.iterator() method to return an Iterator object which can be used to traverse over all elements of the ArrayList as the following example Java code.
ArrayListIteratorExample1.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListIteratorExample1 {
public static void main(String... args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Kotlin");
list.add("Groovy");
// Iterate over elements of an ArrayList
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
String item = iterator.next();
System.out.println(item);
}
}
}
Java
Kotlin
Groovy
Happy Coding 😊
Related Articles
Java Convert Array to ArrayList
Java Insert Element to ArrayList at Specified Index
Java Get Minimum Value in ArrayList
How to Traverse ArrayList using for each loop in Java
How to Traverse ArrayList using forEach() method in Java
Java Convert ArrayList to LinkedList
Java Convert ArrayList to Array
Java Convert ArrayList to String
Java Convert ArrayList to Comma Separated String
Java Convert ArrayList to HashSet
Java Create New Array with Class Type and Length
Java Create ArrayList using List Interface