-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIteratorOps1.java
50 lines (49 loc) · 1.16 KB
/
IteratorOps1.java
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
import java.util.*;
interface Iterator{
public boolean has_next();
public Object get_next();
}
class Sequence{
private SeqIterator _iter =null;
int[] iArr;
int size;
public Sequence(int size_){
iArr=new int[80];
size=0;
}
public void addTo(int elem){
iArr[size]=elem;
size++;
}
public Iterator get_Iterator() {
_iter = new SeqIterator();
return _iter;
}
private class SeqIterator implements Iterator{
int indx;
public SeqIterator(){
indx = -1;
}
public boolean has_next() {
if(indx < size - 1)
return true;
return false;
}
public Object get_next() {
return iArr[++indx];
}
}
}
public class IteratorOps1{
public static void main(String[] args) {
Sequence sObj = new Sequence(5);
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
sObj.addTo(sc.nextInt());
}
sc.close();
Iterator i = sObj.get_Iterator();
while(i.has_next())
System.out.print(i.get_next() + ", ");
}
}