Iterate through ArrayList in Java



ArrayList is a part of the Java Collections Framework. It is used to store elements in a dynamic array that can grow as needed. Unlike arrays, ArrayLists can change their size dynamically, which makes them easier for storing collections of objects without fixing the size at the time of creation.

Our task is to iterate through an ArrayList in Java. Let's look at some scenarios to understand the problem better:

Scenario 1

Input : [1, 2, 3, 4, 5]
Output: 1 2 3 4 5
Explanation: We will iterate through the ArrayList and print each element.

Scenario 2

Input : [10, 20, 30, 40]
Output: 10 20 30 40

Different Ways to Iterate through a Java ArrayList

In Java, there are several ways to iterate through an ArrayList. Here are some common ways to do it:

Using a for loop

The simple way to iterate over an ArrayList is by using a for loop. We will start from the first index (0) and go up to the last index (size - 1) of the ArrayList.

Example

In the below example, we will create an ArrayList of Integer type and then iterate over it using a for loop.

import java.util.ArrayList;
import java.util.List;
public class IterateArrayList {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);

      System.out.println("Iterating through ArrayList using for loop:");
      for (int i = 0; i < arrayList.size(); i++) {
         System.out.print(arrayList.get(i) + " ");
      }
   }
}

When you run the above code, it will produce the following output:

Iterating through ArrayList using for loop:
1 2 3 4 5

Using a while loop

Another way to iterate through an ArrayList is by using a while loop. We will use an index variable to keep track of the current position in the ArrayList.

Example

In the below example, we will create an ArrayList of Integer type and then iterate over it using a while loop:

import java.util.ArrayList;
import java.util.List;
public class IterateArrayList {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(10);
      arrayList.add(20);
      arrayList.add(30);
      arrayList.add(40);

      System.out.println("Iterating through ArrayList using while loop:");
      int i = 0;
      while (i < arrayList.size()) {
         System.out.print(arrayList.get(i) + " ");
         i++;
      }
   }
}

When you run the above code, it will produce the following output:

Iterating through ArrayList using while loop:
10 20 30 40

Using a for-each loop

The forEach loop is similar to the for loop, but it is easier to read. It allows us to iterate through each element of the ArrayList without an index variable.

Example

In the below example, we will create an ArrayList of Integer type and then iterate over it using a forEach loop.

import java.util.ArrayList;
import java.util.List;
public class IterateArrayList {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);

      System.out.println("Iterating through ArrayList using forEach loop:");
      for(int element : arrayList) {
         System.out.print(element + " ");
      }
   }
}

When you run the above code, it will produce the following output:

Iterating through ArrayList using forEach loop:
1 2 3 4 5

Using Iterator

We can retrieve an Iterator object of any Collection. Iterator provides methods such as next() and hasNext(). The hasNext() method returns true if the current Iterator object contains another element, and the next() method returns the next element.

To traverse through the elements of an ArrayList, we can retrieve an iterator object from it using the iterator() method and traverse its elements.

Example

In the below example, we will create an ArrayList of Integer type and then iterate over it using an Iterator.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IterateArrayList {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);

      System.out.println("Iterating through ArrayList using Iterator:");
      Iterator<Integer> it = arrayList.iterator();
      while (it.hasNext()) {
         System.out.print(it.next() + " ");
      }
   }
}

When you run the above code, it will produce the following output:

Iterating through ArrayList using Iterator:
1 2 3 4 5

Using Streams

Java8 introduced the Streams API, which allows us to process collections in a functional style. We can use the forEach method of the Stream interface to iterate through an ArrayList.

Example

In the below example, we will create an ArrayList of Integer type, and then we will create a stream from the ArrayList using the stream() method, and then we will use the forEach method to iterate over it.

import java.util.ArrayList;
import java.util.List;
public class IterateArrayList {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);

      System.out.println("Iterating through ArrayList using Streams:");
      arrayList.stream().forEach(element -> System.out.print(element + " "));
   }
}

When you run the above code, it will produce the following output:

Iterating through ArrayList using Streams:
1 2 3 4 5

Conclusion

In this article, we have learned how to iterate through an ArrayList in Java using different methods such as for loop, while loop, for-each loop, Iterator, and Streams API. Each method has its own advantages and can be used based on our requirements.

Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-07-28T15:18:30+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements