Iterating over ArrayLists in Java
Prerequisite : ArrayList in Java
Method 1 : Using for loop :
// Java program to iterate over an ArrayList// using for loopimport java.util.*;class GFG { public static void main(String[] args) { // initializing ArrayList List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); // For Loop for iterating ArrayList for (int i = 0; i < numbers.size(); i++) System.out.print(numbers.get(i) + " "); }} |
Output:
1 2 3 4 5 6 7 8
Method 2: Using for each loop
// Java program to iterate// over an arraylist using for Each loopimport java.util.*;class GFG { public static void main(String[] args) { // initializing ArrayList List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); // For Each Loop for iterating ArrayList for (Integer i : numbers) System.out.print(i + " "); }} |
Output:
1 2 3 4 5 6 7 8
Method 3 : Using Iterator
// Java program to iterate over an arraylist// using Iteratorimport java.util.*;class GFG { public static void main(String[] args) { // initializing ArrayList List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); // Looping ArrayList using Iterator Iterator it = numbers.iterator(); while (it.hasNext()) System.out.print(it.next() + " "); }} |
Output:
1 2 3 4 5 6 7 8
Method 3 : Using For Each Method of Java 8
// Java program to iterate over an arraylist// using Iteratorimport java.util.*;class GFG { public static void main(String[] args) { // initializing ArrayList List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); //lambda numbers.forEach(number->System.out.println(number)); }} |
Output:
1 2 3 4 5 6 7 8
Removing Items during Traversal :
It is not recommended to use ArrayList.remove() when iterating over elements. This may lead to ConcurrentModificationException (Refer this for a sample program with this exception). When iterating over elements, it is recommended to use Iterator.remove() method .
// Java program to demonstrate working of// Iterator.remove() on an integer arraylistimport java.util.List;import java.util.ArrayList;import java.util.Iterator; public class GFG{ public static void main(String[] args) { List<Integer> al = new ArrayList<Integer>(); al.add(10); al.add(20); al.add(30); al.add(1); al.add(2); // Remove elements smaller than 10 using // Iterator.remove() Iterator itr = al.iterator(); while (itr.hasNext()) { int x = (Integer)itr.next(); if (x < 10) itr.remove(); } System.out.println("Modified ArrayList : " + al); }} |
This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



