-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC45_IterateArrayList.java
More file actions
34 lines (28 loc) · 1 KB
/
C45_IterateArrayList.java
File metadata and controls
34 lines (28 loc) · 1 KB
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
28
29
30
31
32
33
34
import java.util.ArrayList;
import java.util.Iterator;
public class C45_IterateArrayList {
public static void main(String[] args) {
Name.info(); // Print name and enrollment number
// Create an ArrayList of Strings
ArrayList<String> colors = new ArrayList<>();
// Add some colors
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Yellow");
colors.add("Orange");
System.out.println("Iterating using for-each loop:");
for (String color : colors) {
System.out.println(color);
}
System.out.println("\nIterating using Iterator:");
Iterator<String> it = colors.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("\nIterating using for loop with index:");
for (int i = 0; i < colors.size(); i++) {
System.out.println(colors.get(i));
}
}
}