Different Ways to Iterate an ArrayList in Java

In this post we’ll see different ways to iterate an ArrayList in Java. Your options to iterate an ArrayList are as follows-

  1. Using normal for loop
  2. Using For-Each loop (Advanced for loop), available from Java 5
  3. Using Iterator or ListIterator (Use ListIterator only if you want to iterate both forward and backward rather than looping an ArrayList sequentially).
  4. Using forEach statement available from Java 8

Example code to iterate an ArrayList in Java

Here is a Java example code that shows all of the above mentioned ways to loop an ArrayList.


So these are the options for iterating an ArrayList in Java. If you just want to loop through the list sequentially for-each loop is the best option. If you want to modify while iterating then Iterator or ListIterator should be used otherwise any Structural modification to the list will result in ConcurrentModificationException being thrown.

That’s all for the topic Different Ways to Iterate an ArrayList in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

One Comment

  1. That is a good tip especially to those new to the blogosphere.
    Simple but very accurate info… Thank you for sharing this one.
    A must read post!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.