|
@@ -1,4 +1,6 @@
|
1
|
|
-public class MySet<T> extends MyArrayList<T> {
|
|
1
|
+import java.util.Iterator;
|
|
2
|
+
|
|
3
|
+public class MySet<T> extends MyArrayList<T> implements Iterable<T> {
|
2
|
4
|
|
3
|
5
|
public MySet() {
|
4
|
6
|
|
|
@@ -24,7 +26,40 @@ public class MySet<T> extends MyArrayList<T> {
|
24
|
26
|
setMyArrayList(tempArray1);
|
25
|
27
|
setSize(getSize() + 1);
|
26
|
28
|
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ public class SetIterator implements Iterator<T> {
|
|
33
|
+ int current = 0;
|
|
34
|
+
|
|
35
|
+ public boolean hasNext() {
|
|
36
|
+ if (current < getSize()) {
|
|
37
|
+ return true;
|
|
38
|
+ }
|
|
39
|
+ return false;
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ @Override
|
|
43
|
+ public T next() {
|
|
44
|
+ if (!hasNext()) {
|
|
45
|
+ throw new ArrayIndexOutOfBoundsException();
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ return (T) getMyArrayList()[current++];
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ @Override
|
|
52
|
+ public void remove() {
|
|
53
|
+ // removes current -1
|
|
54
|
+ if (current < getSize() - 1) {
|
|
55
|
+ MySet.this.remove(next());
|
|
56
|
+ }
|
27
|
57
|
|
28
|
58
|
}
|
29
|
59
|
}
|
|
60
|
+
|
|
61
|
+ @Override
|
|
62
|
+ public Iterator<T> iterator() {
|
|
63
|
+ return new SetIterator();
|
|
64
|
+ }
|
30
|
65
|
}
|