Przeglądaj źródła

MyArrayList isEmpty()

Trinh Tong 6 lat temu
rodzic
commit
3411a5dc35
2 zmienionych plików z 31 dodań i 4 usunięć
  1. 18
    4
      src/main/java/MyArrayList.java
  2. 13
    0
      src/test/java/MyArrayListTest.java

+ 18
- 4
src/main/java/MyArrayList.java Wyświetl plik

@@ -16,13 +16,17 @@ public class MyArrayList<T> {
16 16
 
17 17
     public void add(T data) {
18 18
 
19
-            Object[] tempArray = new Object[getSize() + 1];
20
-            tempArray[size] = data;
21
-            this.myArrayList = tempArray;
22
-            setSizeBy1();
19
+        Object[] tempArray = new Object[getSize() + 1];
20
+        tempArray[size] = data;
21
+        this.myArrayList = tempArray;
22
+        setSizeBy1();
23 23
 
24 24
     }
25 25
 
26
+    public void add(T data, int index) {
27
+        
28
+    }
29
+
26 30
     public int getSize() {
27 31
         return this.size;
28 32
     }
@@ -79,4 +83,14 @@ public class MyArrayList<T> {
79 83
     public boolean isEmpty() {
80 84
         return (getSize() == 0);
81 85
     }
86
+
87
+    public boolean contains(T data) {
88
+
89
+        for (int i = 0; i < getSize(); i++) {
90
+            if (myArrayList[i].equals(data))
91
+                return true;
92
+        }
93
+
94
+        return false;
95
+    }
82 96
 }

+ 13
- 0
src/test/java/MyArrayListTest.java Wyświetl plik

@@ -68,4 +68,17 @@ public class MyArrayListTest {
68 68
     public void testIsEmpty() {
69 69
         Assert.assertFalse(stringList.isEmpty());
70 70
     }
71
+
72
+    @Test
73
+    public void testContainsTrue() {
74
+        String expectedString = "Wilhelm spilled the tea";
75
+
76
+        Assert.assertTrue(stringList.contains(expectedString));
77
+    }
78
+
79
+    @Test
80
+    public void testContainsFalse() {
81
+        String expectedString = "they moved the table";
82
+        Assert.assertFalse(stringList.contains(expectedString));
83
+    }
71 84
 }