Write a Java Program to Iterate over an ArrayList

In Java, an ArrayList is a dynamic array-like data structure that can be used to store a collection of elements.

It’s a widely used collection class in Java that provides an ordered and resizable list of objects.

If you have an ArrayList and want to iterate over its elements, there are several ways to do that.


One of the easiest and most common ways to iterate over an ArrayList is by using a for loop.

Here’s how you can do it:

import java.util.ArrayList;

public class ArrayListIteration {
    public static void main(String[] args) {
        ArrayList<String> myArrayList = new ArrayList<>();

        myArrayList.add("John");
        myArrayList.add("Mary");
        myArrayList.add("Bob");
        myArrayList.add("Alice");

        for (int i = 0; i < myArrayList.size(); i++) {
            System.out.println(myArrayList.get(i));
        }
    }
}

In the above code, we first create an ArrayList called myArrayList and add four elements to it using the add() method.

Then, we use a for loop to iterate over the ArrayList.

The loop runs from 0 to the size of the ArrayList minus 1 (myArrayList.size() - 1).

We use the get() method to access each element of the ArrayList and print it to the console using System.out.println().

Another way to iterate over an ArrayList is by using a for-each loop.

Here’s an example:

import java.util.ArrayList;

public class ArrayListIteration {
    public static void main(String[] args) {
        ArrayList<String> myArrayList = new ArrayList<>();

        myArrayList.add("John");
        myArrayList.add("Mary");
        myArrayList.add("Bob");
        myArrayList.add("Alice");

        for (String element : myArrayList) {
            System.out.println(element);
        }
    }
}

In the above code, we use a for-each loop to iterate over the ArrayList.

The loop iterates over each element of the ArrayList and assigns it to the variable element.

Then, we simply print the value of element to the console using System.out.println().

Finally, you can also use an Iterator to iterate over an ArrayList. Here’s an example:

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListIteration {
    public static void main(String[] args) {
        ArrayList<String> myArrayList = new ArrayList<>();

        myArrayList.add("John");
        myArrayList.add("Mary");
        myArrayList.add("Bob");
        myArrayList.add("Alice");

        Iterator<String> iterator = myArrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

In the above code, we first create an Iterator using the iterator() method of the ArrayList.

Then, we use a while loop to iterate over the ArrayList.

The loop runs as long as the Iterator has more elements (iterator.hasNext()).

Inside the loop, we use the next() method of the Iterator to access the next element of the ArrayList and print it to the console using System.out.println().


In conclusion, there are several ways to iterate over an ArrayList in Java, including using a for loop, a for-each loop, and an Iterator.

Depending on your specific use case, one of these methods may be more suitable than the others.