-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice21.java
More file actions
113 lines (102 loc) · 2.79 KB
/
Practice21.java
File metadata and controls
113 lines (102 loc) · 2.79 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Cursor: It is an iterator, which is used to iterate or traverse or retrieve a collections or streams obj's elements one by one.
There are 3 types of cursor in java:
- Enumeration
- Iterator
- List Iterator
- Enumeration
It a an interface.
Only available for legacy classes(vector and stack)
Methods:
boolean hasMoreElements()
E nextElement()
- Iterator
It is also an inteface.
It is available for all collection implemented class.
Enumeration and interator both can move in single direction.
It has only read and remove operations are available. Not able to add and replace new obj.
Methods:
boolean hasNext()
obj next()
void remove()
- List Iterator
It is also an inteface.
It is available for all collection implemented class.
List interator can move in both the direction.
It has multiple operations such as read, remove, add and replace.
Methods:
boolean hasNext() void add(E e)
obj(E) next() void set(E e)
void remove() nextIndext()
boolean hasPrevious() PreviousIndex()
obj precious()
*/
import java.util.*;
class Practice21
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("C");
v.add("D");
v.add("E");
v.add("F");
// Enumeration
System.out.println("Using Enumeration");
Enumeration k = v.elements(); // Give infomation to k.
while(k.hasMoreElements())
{
System.out.println(k.nextElement());
}
// Iterator
System.out.println("Using Iterator");
Iterator i = v.iterator();
while(i.hasNext())
{
// System.out.println(i.next());
String s = (String)i.next();
if(s == "C" || s == "A")
{
i.remove();
}
else
{
System.out.println(s);
}
}
// List Iterator
System.out.println("Using List Iterator.");
ListIterator l = v.listIterator();
// while(l.hasNext())
// {
// System.out.println(l.next());
// }
while(l.hasNext())
{
String s = (String)l.next();
if(s.equals("B"))
{
l.set("Java Programming");
}
else if(s.equals("D"))
{
l.add("C Programming");
}
else if(s.equals("E"))
{
l.remove();
}
}
l = v.listIterator(); // Point to the first element again
while(l.hasNext())
{
System.out.println(l.next());
}
while(l.hasPrevious())
{
System.out.println(l.previous());
}
}
}