The three forms of looping are nearly identical. The enhanced for loop:

for (E element : list) {
    . . .
}

is, according to the Java Language Specification, identical in effect to the explicit use of an iterator with a traditional for loop. In the third case, you can only modify the list contents by removing the current element and, then, only if you do it through the remove method of the iterator itself. With index-based iteration, you are free to modify the list in any way. However, adding or removing elements that come before the current index risks having your loop skipping elements or processing the same element multiple times; you need to adjust the loop index properly when you make such changes.

In all cases, element is a reference to the actual list element. None of the iteration methods makes a copy of anything in the list. Changes to the internal state of element will always be seen in the internal state of the corresponding element on the list.

Essentially, there are only two ways to iterate over a list: by using an index or by using an iterator. The enhanced for loop is just a syntactic shortcut introduced in Java 5 to avoid the tedium of explicitly defining an iterator. For both styles, you can come up with essentially trivial variations using for, while or do while blocks, but they all boil down to the same thing (or, rather, two things).

EDIT: As @iX3 points out in a comment, you can use a ListIterator to set the current element of a list as you are iterating. You would need to use List#listIterator() instead of List#iterator() to initialize the loop variable (which, obviously, would have to be declared a ListIterator rather than an Iterator).

Answer from Ted Hopp on Stack Overflow
Top answer
1 of 13
337

The three forms of looping are nearly identical. The enhanced for loop:

for (E element : list) {
    . . .
}

is, according to the Java Language Specification, identical in effect to the explicit use of an iterator with a traditional for loop. In the third case, you can only modify the list contents by removing the current element and, then, only if you do it through the remove method of the iterator itself. With index-based iteration, you are free to modify the list in any way. However, adding or removing elements that come before the current index risks having your loop skipping elements or processing the same element multiple times; you need to adjust the loop index properly when you make such changes.

In all cases, element is a reference to the actual list element. None of the iteration methods makes a copy of anything in the list. Changes to the internal state of element will always be seen in the internal state of the corresponding element on the list.

Essentially, there are only two ways to iterate over a list: by using an index or by using an iterator. The enhanced for loop is just a syntactic shortcut introduced in Java 5 to avoid the tedium of explicitly defining an iterator. For both styles, you can come up with essentially trivial variations using for, while or do while blocks, but they all boil down to the same thing (or, rather, two things).

EDIT: As @iX3 points out in a comment, you can use a ListIterator to set the current element of a list as you are iterating. You would need to use List#listIterator() instead of List#iterator() to initialize the loop variable (which, obviously, would have to be declared a ListIterator rather than an Iterator).

2 of 13
55

Example of each kind listed in the question:

ListIterationExample.java

import java.util.*;

public class ListIterationExample {

     public static void main(String []args){
        List<Integer> numbers = new ArrayList<Integer>();

        // populates list with initial values
        for (Integer i : Arrays.asList(0,1,2,3,4,5,6,7))
            numbers.add(i);
        printList(numbers);         // 0,1,2,3,4,5,6,7

        // replaces each element with twice its value
        for (int index=0; index < numbers.size(); index++) {
            numbers.set(index, numbers.get(index)*2); 
        }
        printList(numbers);         // 0,2,4,6,8,10,12,14

        // does nothing because list is not being changed
        for (Integer number : numbers) {
            number++; // number = new Integer(number+1);
        }
        printList(numbers);         // 0,2,4,6,8,10,12,14  

        // same as above -- just different syntax
        for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            number++;
        }
        printList(numbers);         // 0,2,4,6,8,10,12,14

        // ListIterator<?> provides an "add" method to insert elements
        // between the current element and the cursor
        for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            iter.add(number+1);     // insert a number right before this
        }
        printList(numbers);         // 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

        // Iterator<?> provides a "remove" method to delete elements
        // between the current element and the cursor
        for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            if (number % 2 == 0)    // if number is even 
                iter.remove();      // remove it from the collection
        }
        printList(numbers);         // 1,3,5,7,9,11,13,15

        // ListIterator<?> provides a "set" method to replace elements
        for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            iter.set(number/2);     // divide each element by 2
        }
        printList(numbers);         // 0,1,2,3,4,5,6,7
     }

     public static void printList(List<Integer> numbers) {
        StringBuilder sb = new StringBuilder();
        for (Integer number : numbers) {
            sb.append(number);
            sb.append(",");
        }
        sb.deleteCharAt(sb.length()-1); // remove trailing comma
        System.out.println(sb.toString());
     }
}
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ iterate-through-list-in-java
Iterate through List in Java - GeeksforGeeks
July 23, 2025 - ListIterator is an iterator in Java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ ways to iterate over a list in java
Ways to Iterate Over a List in Java | Baeldung
June 27, 2025 - Weโ€™ll focus on iterating through the list in order, though going in reverse is simple, too. Learn look at how to iterate over the elements of a <em>Set</em> in Java.
๐ŸŒ
Crunchify
crunchify.com โ€บ java j2ee tutorials โ€บ how to iterate through java list? seven (7) ways to iterate through loop in java
How to iterate through Java List? Seven (7) ways to Iterate Through Loop in Java โ€ข Crunchify
December 28, 2025 - How to iterate through Java List? This tutorial demonstrates the use of ArrayList, Iterator and a List. There are 7 ways you can iterate through List.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ iterate-over-arraylist
Java Program to Iterate over an ArrayList
Here, we have used the for-each loop to iterate over the ArrayList and print each element. import java.util.ArrayList; import java.util.ListIterator; class Main { public static void main(String[] args) { // Creating an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(3); numbers.add(2); System.out.println("ArrayList: " + numbers); // Creating an instance of ListIterator ListIterator<Integer> iterate = numbers.listIterator(); System.out.println("Iterating over ArrayList:"); while(iterate.hasNext()) { System.out.print(iterate.next() + ", "); } } }
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ iterate through a collection in java
Iterate through a Collection in Java - HowToDoInJava
February 23, 2023 - With recent coding trends, using streams allow us to iterate the list items, perform operations on items, and collect the results in a seamless manner. List<String> list = List.of("A", "B", "C", "D"); list.stream() //other operation as needed .forEach(System.out::println); Recently introduced in java 8, this method can be called on any Iterable and takes one argument implementing the functional interface java.util.function.Consumer.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-iterate-over-a-list-in-java
How to iterate over a list in Java
You can iterate over a list with a simple for loop. A for loop has three parts to it: the initializing statement, the loop condition check statement, and the increment statement, as shown below:
๐ŸŒ
Medium
medium.com โ€บ @reetesh043 โ€บ how-to-iterate-arraylist-in-java-with-examples-c66be52c8211
How to Iterate ArrayList in Java with Examples | by Reetesh Kumar | Medium
January 7, 2024 - Our primary focus will be on iterating through the list in its original order, but weโ€™ll also touch on the straightforward process of traversing it in reverse. Letโ€™s assume we have a simple banking system with a list of bank accounts. Each bank account has a balance, an account number, and a customerโ€™s name. We will use the different Java features to iterate over this list of bank accounts in various ways.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @yadavsunil9699 โ€บ exploring-various-ways-to-iterate-through-lists-in-java-a3b2fe4cdd9c
Exploring Various Ways to Iterate Through Lists in Java | by Yadavsunil | Medium
September 8, 2023 - In this post, Iโ€™ll walk you through several ways to iterate through a list in Java, complete with code examples.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 532124 โ€บ java โ€บ iterate-list-loop-enhanced-loop
How do iterate 2 list's using for-each loop(enhanced for loop) (Java in General forum at Coderanch)
March 26, 2011 - One assumes that once the double-iteration problem is solved, he might want to do something more interesting than just print the values. This might well require accessing methods defined for the Dept and Emp classes. Wouter Oet wrote:Anyway even if you where casting, an enhanced for-loop doesn't do anything like that. The loop only takes an Iterator and iterates that. Allowing you to access the elements of the data-source. Ummm... an enhanced for loop also uses the generic type of the List (or more generally, Iterable) and allows us to treat elements from that List as instances of the appropriate type.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-iterate-over-a-Java-list
How to iterate over a Java list?
July 30, 2019 - In general, to use an iterator to cycle through the contents of a collection, follow these steps โˆ’ ยท Obtain an iterator to the start of the collection by calling the collection's iterator() method. Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns ...
๐ŸŒ
Java67
java67.com โ€บ 2012 โ€บ 07 โ€บ how-to-iterate-loop-traverse-list-java.html
3 Examples to Loop over a List in Java - ArrayList, LinkedList or Vector | Java67
Now the question is whether should you use the Iterator or enhanced for loop, or the forEach() method of Java 8 for looping over List in Java. Well, it depends on what you are doing with the object, if you need to remove some objects from List then iterating using Iterator is the best choice to avoid ConcurrentModificationException, but if you are not removing any element and just doing some operation with each element than enhanced for loop is much cleaner ways to do that.
๐ŸŒ
DevGenius
blog.devgenius.io โ€บ 7-ways-to-iterate-over-a-list-in-java-from-classic-loops-to-modern-approaches-caefdd93204b
7 Ways to Iterate Over a List in Java: From Classic Loops to Modern Approaches | by M Business Solutions | Dev Genius
December 5, 2024 - The most basic and traditional way to iterate over a list is by using a standard for loop with an index. This method gives you full control over the iteration process. import java.util.ArrayList; import java.util.List; public class ForLoopExample ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-iterate-over-a-list-in-java
How to iterate over a list in Java?
May 26, 2025 - In the following example, we iterate over a List using a for-loop and use the get(index) method inside the loop to access each element based on the indexed value passed to it: import java.util.ArrayList; import java.util.List; public class iterateOverList { public static void main(String[] args) { //instantiating a List using ArrayList class List<Integer> list = new ArrayList<>(); //adding element to it list.add(1); list.add(2); list.add(3); list.add(4); System.out.println("The list elements are: "); //iterate over a list using for-loop for(int i = 0; i<list.size(); i++){ System.out.println(list.get(i)); } } }
๐ŸŒ
springjava
springjava.com โ€บ core-java โ€บ how-to-iterate-list-in-java
How to Iterate List in Java | Spring Java
February 27, 2024 - import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String args[]){ //Creating an ArrayList List<String> theList = new ArrayList<String>(); //Adding elements to the List theList.add("Peter"); theList.add("Robert"); theList.add("John"); theList.add("Parker"); int i=0; //Using Iterator to iterate the List Iterator<String> it = theList.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
๐ŸŒ
Medium
medium.com โ€บ javarevisited โ€บ i-need-an-index-with-this-list-iteration-method-1e339fd55ed7
I need an index with this List iteration method | by Donald Raab | Javarevisited | Medium
February 10, 2024 - In this example, the else part of the if statement will execute, since LinkedList is not a RandomAccess List. Since Java 8, it is possible to use IntStream.range() to iterate over a set of indices and use List look ups using get().
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-iterate-a-list-using-for-loop-in-java
How to iterate a List using for Loop in Java?
May 26, 2025 - In the following example, we iterate over a List (instantiating using ArrayList class) using a for-loop to print each element one by one of the List {10, 20, 30, 40}: import java.util.ArrayList; import java.util.List; public class iterateOverList { public static void main(String[] args) { //instantiating a List using ArrayList class List<Integer> list = new ArrayList<>(); //adding element to it list.add(10); list.add(20); list.add(30); list.add(40); System.out.println("The list elements are: "); //iterate over a list using for-loop for(int i= 0; i<list.size(); i++) { System.out.print(list.get(i) + " "); } } }
๐ŸŒ
ConcretePage
concretepage.com โ€บ java โ€บ iterate-list-java
How to Iterate List in Java
Ex.2: To write multiple line while iterating use {} as below. List<Integer> list = List.of(10, 20, 30); list.forEach(n -> { int res = n * n; System.out.println(res); }); Ex.3: Iterate list of objects of custom class. import java.util.List; public class JavaExamples { public static void main(String[] args) { List<Student> list = List.of(new Student("Mahesh", 30), new Student("John", 35), new Student("Lily", 25)); list.forEach(s -> System.out.println(s.name + "-" + s.age)); } } class Student { String name; Integer age; public Student(String name, Integer age) { this.name = name; this.age = age; } } Output
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ different ways to iterate an arraylist
Different Ways to Iterate an ArrayList
January 12, 2023 - We can apply these iteration examples on any List, storing any type of object. We will use these five ways to loop through ArrayList. ... Java program to iterate through an ArrayList of objects using the standard for loop. ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") ); for(int i = 0; i < namesList.size(); i++) { System.out.println(namesList.get(i)); }
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-loop-arraylist-in-java
How to loop ArrayList in Java
An Iterator provides a way to iterate a collection, and it allows you to remove an element while iterating. import java.util.ArrayList; import java.util.Iterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Chaitanya"); names.add("Rahul"); names.add("Aditya"); Iterator<String> iterator = names.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } } A ListIterator allows you to traverse the list in both directions.