Java ArrayList for loop for each example shows how to iterate ArrayList using for loop and for each loop in Java. It also shows how to use the ArrayList size to loop through the elements of ArrayList.
How to iterate ArrayList using for loop and for each loop in Java?
You can use the size
method of ArrayList to get total number of elements in ArrayList and the get
method to get the element at the specified index from ArrayList.
Using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListForLoopForEachExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListShapes = new ArrayList<String>(); //add objects to ArrayList aListShapes.add("Circle"); aListShapes.add("Square"); aListShapes.add("Rectangle"); aListShapes.add("Oval"); System.out.println("Iterate ArrayList using for loop"); //iterate ArrayList using for loop for(int i = 0; i < aListShapes.size(); i++){ System.out.println( aListShapes.get(i) ); } } } |
Output
1 2 3 4 5 |
Iterate ArrayList using for loop Circle Square Rectangle Oval |
ArrayList index starts from 0, so we initialized our index variable i
with 0 and looped until it reaches the ArrayList size – 1 index. Inside the loop we print the elements of ArrayList using the get
method.
Using enhanced for loop
Iterating over ArrayList using enhanced for loop is a bit different from iterating ArrayList using for loop. When we use the enhanced for loop, we do not need to maintain the index variable as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListForLoopForEachExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListShapes = new ArrayList<String>(); //add objects to ArrayList aListShapes.add("Circle"); aListShapes.add("Square"); aListShapes.add("Rectangle"); aListShapes.add("Oval"); System.out.println("Iterate ArrayList using enhanced for loop"); //iterate ArrayList using for each for( String shape : aListShapes ) System.out.println( shape ); } } |
Output
1 2 3 4 5 |
Iterate ArrayList using enhanced for loop Circle Square Rectangle Oval |
Using forEach loop (Java 8)?
If you are using Java 8, you can use the forEach
to iterate through the List as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//create new ArrayList ArrayList<String> aListShapes = new ArrayList<String>(); //add objects to ArrayList aListShapes.add("Circle"); aListShapes.add("Square"); aListShapes.add("Rectangle"); aListShapes.add("Oval"); System.out.println("Iterate ArrayList using forEach loop"); //iterate ArrayList using forEach aListShapes.forEach(shape->System.out.println(shape)); |
Output
1 2 3 4 5 |
Iterate ArrayList using forEach loop Circle Square Rectangle Oval |
You can also visit how to iterate over List example to learn about iterating over List using several ways apart from using for loop and for each loop.
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.