Iterating over ArrayLists in Java
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) + " "); } } |
chevron_right
filter_none
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 + " "); } } |
chevron_right
filter_none
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() + " "); } } |
chevron_right
filter_none
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)); } } |
chevron_right
filter_none
Output:
1 2 3 4 5 6 7 8
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.
Recommended Posts:
- Join two ArrayLists in Java
- Find common elements in two ArrayLists in Java
- 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
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
Improved By : SandeepChaudhary



