Trinh Tong преди 6 години
родител
ревизия
9148eac370
променени са 4 файла, в които са добавени 90 реда и са изтрити 37 реда
  1. 2
    2
      src/main/java/MyArrayList.java
  2. 31
    27
      src/main/java/MySet.java
  3. 0
    7
      src/test/java/MyArrayListTest.java
  4. 57
    1
      src/test/java/MySetTest.java

+ 2
- 2
src/main/java/MyArrayList.java Целия файл

@@ -17,8 +17,8 @@ public class MyArrayList<T> {
17 17
         this.size = size;
18 18
     }
19 19
 
20
-    public Object[] getMyArrayList() {
21
-        return myArrayList;
20
+    public T[] getMyArrayList() {
21
+        return (T[]) myArrayList;
22 22
     }
23 23
 
24 24
     public void add(T data) {

+ 31
- 27
src/main/java/MySet.java Целия файл

@@ -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
 }

+ 0
- 7
src/test/java/MyArrayListTest.java Целия файл

@@ -31,13 +31,6 @@ public class MyArrayListTest {
31 31
 
32 32
 
33 33
         stringList.add(1, "Dolio Rollio");
34
-
35
-        for (int i = 0; i < stringList.getSize(); i++) {
36
-            System.out.println(stringList.get(i));
37
-        }
38
-
39
-//        System.out.println(stringList.get(1));
40
-
41 34
         Assert.assertTrue(expectedString.equals(stringList.get(1)));
42 35
 
43 36
     }

+ 57
- 1
src/test/java/MySetTest.java Целия файл

@@ -2,6 +2,10 @@ import org.junit.Assert;
2 2
 import org.junit.Before;
3 3
 import org.junit.Test;
4 4
 
5
+import java.util.Collection;
6
+import java.util.Iterator;
7
+import java.util.Set;
8
+
5 9
 public class MySetTest {
6 10
 
7 11
     MySet<Integer> mySet;
@@ -14,7 +18,7 @@ public class MySetTest {
14 18
 
15 19
     @Test
16 20
     public void testAddUnique() {
17
-        int expectedOutput = 1;
21
+        Integer expectedOutput = 1;
18 22
         mySet.add(1);
19 23
         Assert.assertEquals(expectedOutput, mySet.getMyArrayList()[mySet.getSize()-1]);
20 24
     }
@@ -51,5 +55,57 @@ public class MySetTest {
51 55
         Assert.assertFalse(mySet.contains(3));
52 56
     }
53 57
 
58
+    @Test
59
+    public void testHasNextFalse() {
60
+        Assert.assertFalse(mySet.iterator().hasNext());
61
+    }
62
+
63
+    @Test
64
+    public void testHasNextTrue() {
65
+        mySet.add(1);
66
+        mySet.add(2);
67
+        mySet.add(5);
68
+        mySet.add(10);
69
+
70
+        Assert.assertTrue(mySet.iterator().hasNext());
71
+    }
72
+
73
+    @Test
74
+    public void testIteratorNext() {
75
+        Integer expectedNext = 1;
76
+        mySet.add(1);
77
+
78
+        Assert.assertEquals(expectedNext, mySet.iterator().next());
79
+    }
80
+
81
+    @Test
82
+    public void testIteratorRemove() {
83
+        mySet.add(1);
84
+        mySet.add(2);
85
+        mySet.add(5);
86
+        mySet.add(10);
87
+
88
+        mySet.iterator().remove();
89
+
90
+        Assert.assertFalse(mySet.contains(1));
91
+    }
54 92
 
93
+//    @Test
94
+//    public void testContainsAllFalse() {
95
+//        MySet<Integer> inputSet = new MySet<>();
96
+//        inputSet.add(1);
97
+//        inputSet.add(2);
98
+//        inputSet.add(3);
99
+//        inputSet.add(4);
100
+//
101
+//        Object[] inputArray = inputSet.getMyArrayList();
102
+//
103
+//        for (Object i: inputArray
104
+//             ) {
105
+//            System.out.println(i);
106
+//        }
107
+//        // Checks if all the values are in the set, if not add the values;
108
+//        Assert.assertFalse(mySet.containsAll(inputArray));
109
+//
110
+//    }
55 111
 }