-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergingIncreasingIterator.java
More file actions
59 lines (51 loc) · 1.52 KB
/
Copy pathMergingIncreasingIterator.java
File metadata and controls
59 lines (51 loc) · 1.52 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
package seminar1.iterators;
import java.util.Iterator;
/**
* Итератор возвращающий последовательность из двух возрастающих итераторов в порядке возрастания
* first = 1,3,4,5,7
* second = 0,2,4,6,8
* result = 0,1,2,3,4,4,5,6,7,8
*
* Time = O(k),
* k — суммарное количество элементов
*/
public class MergingIncreasingIterator implements Iterator<Integer> {
private IncreasingIterator first;
private IncreasingIterator second;
private Integer one=null;
private Integer two=null;
public MergingIncreasingIterator(IncreasingIterator first, IncreasingIterator second) {
this.first = first;
this.second = second;
if(first.hasNext()) one=first.next();
if(second.hasNext())two=second.next();
}
@Override
public boolean hasNext() {
if(first.hasNext()||second.hasNext()) return true;
return false;
}
@Override
public Integer next() {
int tmp1;
if(one==null)
tmp1= second.next();
else if(two==null)
tmp1=first.next();
else if(one>two) {
tmp1=two;
if(second.hasNext())
two=second.next();
else two=null;
}
else{
tmp1=one;
if(first.hasNext())
one=first.next();
else one=null;
}
return tmp1;
//if(second.hasNext()&&second)
//return null;
}
}