forked from dharmanshu1921/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratorMain.java
More file actions
26 lines (25 loc) · 772 Bytes
/
IteratorMain.java
File metadata and controls
26 lines (25 loc) · 772 Bytes
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorMain {
public static void main(String[] args) {
List<Integer> dll = new ArrayList<Integer>();
dll.add(10);
dll.add(20);
dll.add(30);
dll.add(40);
dll.add(50);
dll.add(60);
System.out.println("For Each Method:");
for (Integer a : dll) {// foreach
System.out.print(a + " ");
}
System.out.println();
System.out.println("Iterator Method:");
Iterator<Integer> it = dll.iterator();// after adding all elements we need to declare it
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
}
}