How to iterate ArrayList in Java


This tutorial will explain how you can iterate an ArrayList in Java. Initially, we always use for loop to iterate any list but in this example, we will cover the six different ways to iterate any ArrayList.

1- Using forEach

We can use the stream API to iterate any ArrayList. forEach method added in Java 8, to use forEach you must need to install JDK 8 in your machine.

forEachExample.java
package org.websparrow;

import java.util.ArrayList;

public class forEachExample {
	public static void main(String[] args) {
		ArrayList<String> cityList = new ArrayList<>();
		cityList.add("Varanasi");
		cityList.add("Delhi");
		cityList.add("Mumbai");
		cityList.add("Kolkata");
		cityList.add("Chennai");
		cityList.add("Bangloru");

		cityList.forEach((mylist) -> {
			System.out.println(mylist);
		});
	}
}

2- Using Iterator

hasNext() – Returns true if the iteration has more elements.

next() – Returns the next element in the iteration.

IteratorExample.java
package org.websparrow;

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

public class IteratorExample {
	public static void main(String[] args) {
		ArrayList<String> cityList = new ArrayList<>();
		cityList.add("Varanasi");
		cityList.add("Delhi");
		cityList.add("Mumbai");
		cityList.add("Kolkata");
		cityList.add("Chennai");
		cityList.add("Bangloru");

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

3- Using ListIterator

ListIterator extends Iterator

ListIteratorExample.java
package org.websparrow;

import java.util.ArrayList;
import java.util.ListIterator;

public class ListIteratorExample {
	public static void main(String[] args) {
		ArrayList<String> cityList = new ArrayList<>();
		cityList.add("Varanasi");
		cityList.add("Delhi");
		cityList.add("Mumbai");
		cityList.add("Kolkata");
		cityList.add("Chennai");
		cityList.add("Bangloru");

		ListIterator<String> listIterator = cityList.listIterator();
		while (listIterator.hasNext()) {
			System.out.println(listIterator.next());
		}
	}
}

4- Using Enhanced For Loop

EnhancedForLoopExample.java
package org.websparrow;

import java.util.ArrayList;

public class EnhancedForLoopExample {
	public static void main(String[] args) {
		ArrayList<String> cityList = new ArrayList<>();
		cityList.add("Varanasi");
		cityList.add("Delhi");
		cityList.add("Mumbai");
		cityList.add("Kolkata");
		cityList.add("Chennai");
		cityList.add("Bangloru");

		for (String str : cityList) {
			System.out.println(str);
		}
	}
}

5- Using For Loop

ForLoopExample.java
package org.websparrow;

import java.util.ArrayList;

public class ForLoopExample {
	public static void main(String[] args) {
		ArrayList<String> cityList = new ArrayList<>();
		cityList.add("Varanasi");
		cityList.add("Delhi");
		cityList.add("Mumbai");
		cityList.add("Kolkata");
		cityList.add("Chennai");
		cityList.add("Bangloru");

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

6- Using While Loop

WhileLoopExample.java
package org.websparrow;

import java.util.ArrayList;

public class WhileLoopExample {
	public static void main(String[] args) {
		ArrayList<String> cityList = new ArrayList<>();
		cityList.add("Varanasi");
		cityList.add("Delhi");
		cityList.add("Mumbai");
		cityList.add("Kolkata");
		cityList.add("Chennai");
		cityList.add("Bangloru");

		int ctr = 0;
		while (ctr < cityList.size()) {
			System.out.println(cityList.get(ctr));
			ctr++;
		}
	}
}

Output:

Varanasi
Delhi
Mumbai
Kolkata
Chennai
Bangloru

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.