-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectArrayIterator.java
43 lines (41 loc) · 1.45 KB
/
ObjectArrayIterator.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
package xycabcd.util;
public class ObjectArrayIterator<T> implements java.util.Iterator<T>, Cloneable {
protected final T[] data;
protected int index;
protected final int last;
public ObjectArrayIterator(T[] d) {
if(d == null) throw new NullPointerException();
data = d.clone();
index = 0;
last = d.length-1;
}
public ObjectArrayIterator(T[] d, int firstIndex) {
if(d == null) throw new NullPointerException();
if(firstIndex < 0) throw new IllegalArgumentException();
if(firstIndex > d.length-1) throw new IllegalArgumentException();
data = d.clone();
index = firstIndex;
last = d.length-1;
}
public ObjectArrayIterator(T[] d, int firstIndex, int lastIndex) {
if(d == null) throw new NullPointerException();
if(firstIndex < 0) throw new IllegalArgumentException();
if(firstIndex > d.length-1) throw new IllegalArgumentException();
if(lastIndex < firstIndex) throw new IllegalArgumentException();
if(lastIndex > d.length-1) throw new IllegalArgumentException();
data = d.clone();
index = firstIndex;
last = lastIndex;
}
@Override
public T next() {
if(!hasNext()) throw new java.util.NoSuchElementException();
index++;
return data[index-1];
}
@Override
public boolean hasNext() {
if(index > last) return false;
return true;
}
}