Prerequisite : ArrayList in Java
Method 1 : Using for loop :
// Java program to iterate over an ArrayList // using for loop import 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 loop import 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 Iterator import 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 Iterator import 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 arraylist import 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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Iterating over Arrays in Java
- Iterating over Enum Values in Java
- Remove an Entry using key from HashMap while Iterating over it
- Remove an Entry using value from HashMap while Iterating over it
- Find common elements in two ArrayLists in Java
- Join two ArrayLists in Java
- Why to use char[] array over a string for storing passwords in Java?
- Program to Iterate over a Stream with Indices in Java 8
- How to loop over TreeSet in Java
- How to iterate over a TreeMap in Java?
- How to iterate over a 2D list (list of lists) in Java
- Java Program to Iterate Over Arrays Using for and foreach Loop
- When to use StringJoiner over StringBuilder?
- Sending a Text Message Over the Phone Using SmsManager in Android
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
Improved By : SandeepChaudhary

