|
@@ -3,33 +3,10 @@ import java.util.Iterator;
|
3
|
3
|
public class MySet<T> extends MyArrayList<T> implements Iterable<T> {
|
4
|
4
|
|
5
|
5
|
public MySet() {
|
6
|
|
-
|
7
|
6
|
}
|
8
|
7
|
|
9
|
8
|
|
10
|
|
- @Override
|
11
|
|
- public void add(T data){
|
12
|
|
- // Sets cannot have duplicates
|
13
|
|
- boolean hasItem = false;
|
14
|
|
-
|
15
|
|
- for (Object o: getMyArrayList()) {
|
16
|
|
- if (o.equals(data)) {
|
17
|
|
- hasItem = true;
|
18
|
|
- break;
|
19
|
|
- }
|
20
|
|
- }
|
21
|
|
-
|
22
|
|
- if (!hasItem) {
|
23
|
|
- Object[] tempArray1 = new Object[getSize() + 1];
|
24
|
|
- copy(tempArray1, getMyArrayList());
|
25
|
|
- tempArray1[getSize()] = data;
|
26
|
|
- setMyArrayList(tempArray1);
|
27
|
|
- setSize(getSize() + 1);
|
28
|
|
-
|
29
|
|
- }
|
30
|
|
- }
|
31
|
|
-
|
32
|
|
- public class SetIterator implements Iterator<T> {
|
|
9
|
+ private class SetIterator implements Iterator<T> {
|
33
|
10
|
int current = 0;
|
34
|
11
|
|
35
|
12
|
public boolean hasNext() {
|
|
@@ -50,9 +27,9 @@ public class MySet<T> extends MyArrayList<T> implements Iterable<T> {
|
50
|
27
|
|
51
|
28
|
@Override
|
52
|
29
|
public void remove() {
|
53
|
|
- // removes current -1
|
54
|
|
- if (current < getSize() - 1) {
|
55
|
|
- MySet.this.remove(next());
|
|
30
|
+ // removes current -1
|
|
31
|
+ if (current < getSize()) {
|
|
32
|
+ MySet.this.remove((T) getMyArrayList()[current]);
|
56
|
33
|
}
|
57
|
34
|
|
58
|
35
|
}
|
|
@@ -62,4 +39,31 @@ public class MySet<T> extends MyArrayList<T> implements Iterable<T> {
|
62
|
39
|
public Iterator<T> iterator() {
|
63
|
40
|
return new SetIterator();
|
64
|
41
|
}
|
|
42
|
+
|
|
43
|
+ public boolean hasItem(T data) {
|
|
44
|
+ for (Object o: getMyArrayList()) {
|
|
45
|
+ if (o.equals(data)) {
|
|
46
|
+ return true;
|
|
47
|
+ }
|
|
48
|
+ }
|
|
49
|
+ return false;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ @Override
|
|
53
|
+ public void add(T data){
|
|
54
|
+ // Sets cannot have duplicates
|
|
55
|
+ boolean hasItem = hasItem(data);
|
|
56
|
+
|
|
57
|
+ if (!hasItem) {
|
|
58
|
+ Object[] tempArray1 = new Object[getSize() + 1];
|
|
59
|
+ copy(tempArray1, getMyArrayList());
|
|
60
|
+ tempArray1[getSize()] = data;
|
|
61
|
+ setMyArrayList(tempArray1);
|
|
62
|
+ setSize(getSize() + 1);
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
65
|
69
|
}
|