EN
Java - loop through ArrayList
0 points
In this article, we would like to show you how to loop through ArrayList in Java.
Quick solution:
xxxxxxxxxx
1
List<String> myArrayList = new ArrayList<>();
2
3
for (String item : myArrayList) {
4
System.out.println(item);
5
}
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
List<String> fruits = new ArrayList<>();
5
6
// add items to the ArrayList
7
fruits.add("Apple");
8
fruits.add("Banana");
9
fruits.add("Cherry");
10
11
// loop through the ArrayList
12
for (int i = 0; i < fruits.size(); i++) {
13
System.out.println(fruits.get(i));
14
}
15
}
16
}
Output:
xxxxxxxxxx
1
Apple
2
Banana
3
Cherry
xxxxxxxxxx
1
import java.util.ArrayList;
2
3
public class Example {
4
5
public static void main(String[] args) {
6
List<String> fruits = new ArrayList<>();
7
8
// add items to the ArrayList
9
fruits.add("Apple");
10
fruits.add("Banana");
11
fruits.add("Cherry");
12
13
// loop through the ArrayList
14
for (String fruit: fruits) {
15
System.out.println(fruit);
16
}
17
}
18
}
Output:
xxxxxxxxxx
1
Apple
2
Banana
3
Cherry