In this guide, you will learn how you can loop through an ArrayList
in Java. In the ArrayList tutorial, we learned that it belongs to java.util
package and unlike arrays, it can grow in size dynamically. There are several different approaches to iterate an ArrayList, lets discuss them with examples:
1. Using a for Loop
One of the easiest way to iterate through an ArrayList
is by using for loop:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Chaitanya");
names.add("Rahul");
names.add("Aditya");
// names.size() returns size of ArrayList and get()
// method returns the element present at specified index
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
}
}
2. Using an Enhanced for
Loop (for-each)
The enhanced for loop provides a simple and more readable way to iterate through all the elements in an ArrayList
. Let’s take the same example that we have seen above but with for-each loop instead of for loop.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Chaitanya");
names.add("Rahul");
names.add("Aditya");
for (String name : names) {
System.out.println(name);
}
}
}
3. Using an Iterator
One of the limitations with for-each loop is that you cannot remove an element while iterating. 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());
}
}
}
4. Using a ListIterator
A ListIterator
allows you to traverse the list in both directions. You can also modify the list during iteration:
import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Chaitanya");
names.add("Rahul");
names.add("Aditya");
ListIterator<String> listIterator = names.listIterator();
// Traversing forward
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
// Traversing backward
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
5. Using Streams (Java 8 and later)
Stream API is introduced in Java 8. I have covered it in detail here: Java 8 Streams Tutorial. You can also loop through an ArrayList elements using stream.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Chaitanya");
names.add("Rahul");
names.add("Aditya");
names.stream().forEach(System.out::println);
}
}
Govardhan says
sir how to iterate arraylist elements using Enumeration interface
Chaitanya Singh says
Hi Govardhan,
I have updated the post and added the code. You can find your answer above in the post. Let me know if you have any further question.
Govardhan says
sir I have one more question:=
1.How to find middle value of a linked list by single pass
srinath says
Use two variable, lets call them fastVariable and slowVariable.
Move the fastVariable twice the speed of slowVariable.
By the time fastVariable reach end of the list slowVariable will be at middle of the list.
Thanks.
David tolu says
how to detect duplicate values in an arrayList?
Melly says
Hello!
So I have two seperate arraylists. I am trying to display both of them on a form, but only one of them is showing up. Is it possible to loop to arrayLists and display them at the same time?
Chaitanya Singh says
Melly, You can join both the arraylists and then loop the combined arraylist to display all the elements. Refer this: How to join ArrayList?