In this example we will show the method to loop through an ArrayList with for-each in Java.
Source Code
package com.beginner.examples;
import java.util.ArrayList;
public class LoopAnArrayList {
public static void main(String[] args) {
ArrayList strs = new ArrayList();
strs.add("a");
strs.add("b");
strs.add("c");
for (String s : strs) { // loop through an ArrayList
System.out.println(s);
}
}
}
Output:
a
b
c
References
Imported packages in Java documentation: