-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratorDemo.java
More file actions
36 lines (36 loc) · 929 Bytes
/
IteratorDemo.java
File metadata and controls
36 lines (36 loc) · 929 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
27
28
29
30
31
32
33
34
35
36
import java.util.*;
class IteratorDemo{
public static void main(String args[]){
ArrayList<String> al = new ArrayList<String>();
al.add("I");
al.add("am");
al.add("not");
al.add("so");
al.add("high");
System.out.print("Original contents of al: ");
Iterator<String> itr = al.iterator();
while(itr.hasNext()){
String element = itr.next();
System.out.print(element+" ");
}
System.out.println();
ListIterator<String> litr = al.listIterator();
while(litr.hasNext()){
String element = litr.next();
litr.set(element+"~");
}
System.out.print("Modified contents of al: ");
itr = al.iterator();
while(itr.hasNext()){
String element = itr.next();
System.out.print(element+"");
}
System.out.println();
System.out.print("Modified list backwards: ");
while(litr.hasPrevious()){
String element = litr.previous();
System.out.print(element+ " ");
}
System.out.println();
}
}