浏览代码

MyArrayList isEmpty()

Trinh Tong 6 年前
父节点
当前提交
60f9af07b0
共有 2 个文件被更改,包括 130 次插入13 次删除
  1. 72
    11
      src/main/java/MyArrayList.java
  2. 58
    2
      src/test/java/MyArrayListTest.java

+ 72
- 11
src/main/java/MyArrayList.java 查看文件

@@ -1,21 +1,82 @@
1
-import java.lang.reflect.Array;
2
-
3 1
 public class MyArrayList<T> {
4
-    private final int INITIAL_SIZE = 10;
2
+    private final static int INITIAL_SIZE = 0;
5 3
     private Object[] myArrayList;
6 4
     private int size;
7 5
 
8
-    // create array of Object[3]
9
-
10 6
     public MyArrayList() {
11
-        this.myArrayList = new Object[INITIAL_SIZE];
12
-        size = INITIAL_SIZE;
7
+        this(INITIAL_SIZE);
8
+        this.size = myArrayList.length;
9
+    }
10
+
11
+    public MyArrayList(int size) {
12
+        this.myArrayList = new Object[size];
13
+        this.size = size;
14
+    }
15
+
16
+
17
+    public void add(T data) {
18
+
19
+            Object[] tempArray = new Object[getSize() + 1];
20
+            tempArray[size] = data;
21
+            this.myArrayList = tempArray;
22
+            setSizeBy1();
23
+
24
+    }
25
+
26
+    public int getSize() {
27
+        return this.size;
28
+    }
29
+
30
+    public void setSizeBy1() {
31
+        this.size++;
32
+    }
33
+
34
+    public void resetSize() {
35
+        this.size = INITIAL_SIZE;
13 36
     }
14 37
 
15
-    public MyArrayList(T[] myArrayList) {
16
-        this.myArrayList = myArrayList;
17
-        size = myArrayList.length;
38
+    public void decreaseSize() {
39
+        this.size--;
18 40
     }
19 41
 
20
-    
42
+    public T get(int i) {
43
+        return (T) this.myArrayList[i];
44
+    }
45
+
46
+    public void remove(T data) {
47
+        // removes first instance of that data
48
+
49
+        Object[] smallerArray = new Object[this.myArrayList.length - 1];
50
+        int counter = 0;
51
+        for (int i = 0; i < myArrayList.length; i++) {
52
+            System.out.println(myArrayList[i]);
53
+            if (!myArrayList[i].equals(data)) {
54
+                smallerArray[counter++] = myArrayList[i];
55
+            }
56
+        }
57
+
58
+        this.myArrayList = smallerArray;
59
+        decreaseSize();
60
+    }
61
+
62
+
63
+    public void set(int i, T data) {
64
+        try {
65
+            this.myArrayList[i] = data;
66
+        } catch (ArrayIndexOutOfBoundsException e) {
67
+            System.out.println("That index is not in my array list!");
68
+        }
69
+    }
70
+
71
+
72
+    public void clear() {
73
+        Object[] tempArr = new Object[INITIAL_SIZE];
74
+        resetSize();
75
+
76
+        myArrayList = tempArr;
77
+    }
78
+
79
+    public boolean isEmpty() {
80
+        return (getSize() == 0);
81
+    }
21 82
 }

+ 58
- 2
src/test/java/MyArrayListTest.java 查看文件

@@ -1,15 +1,71 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
4 5
 public class MyArrayListTest {
5
-    MyArrayList arrayList;
6
+    MyArrayList<String> stringList;
6 7
     @Before
7 8
     public void setUp() {
8
-    arrayList = new MyArrayList();
9
+
10
+        stringList = new MyArrayList<>();
11
+        stringList.add("Wilhelm spilled the tea");
9 12
     }
10 13
 
11 14
     @Test
12 15
     public void testAdd() {
13 16
 
17
+        int expectedSize = 1;
18
+
19
+        Assert.assertEquals(expectedSize, stringList.getSize());
20
+    }
21
+
22
+    @Test
23
+    public void testGet() {
24
+        String expectedString = "Wilhelm spilled the tea";
25
+        Assert.assertTrue(expectedString.equals(stringList.get(stringList.getSize()-1)));
26
+
27
+    }
28
+
29
+    @Test
30
+    public void testRemove() {
31
+        int expectedSize = 0;
32
+        stringList.remove("Wilhelm spilled the tea");
33
+        Assert.assertEquals(expectedSize, stringList.getSize());
34
+    }
35
+
36
+    @Test
37
+    public void testSet() {
38
+        String expectedString = "they moved the table";
39
+
40
+        stringList.add("Wilhelm spilled the tea");
41
+        stringList.add("Wilhelm spilled the tea");
42
+        stringList.add("the table got wet :(");
43
+        stringList.add("Nhu got more tears to fill up her tea");
44
+        stringList.add("Wilhelm spilled the tea");
45
+        stringList.add("Wilhelm spilled the tea");
46
+
47
+        stringList.set(5, "they moved the table");
48
+
49
+        Assert.assertEquals(expectedString, stringList.get(5));
50
+    }
51
+
52
+    @Test
53
+    public void testClear() {
54
+        int expectedSize = 0;
55
+        stringList.add("Wilhelm spilled the tea");
56
+        stringList.add("Wilhelm spilled the tea");
57
+        stringList.add("the table got wet :(");
58
+        stringList.add("Nhu got more tears to fill up her tea");
59
+        stringList.add("Wilhelm spilled the tea");
60
+        stringList.add("Wilhelm spilled the tea");
61
+
62
+        stringList.clear();
63
+        Assert.assertEquals(expectedSize, stringList.getSize());
64
+
65
+    }
66
+
67
+    @Test
68
+    public void testIsEmpty() {
69
+        Assert.assertFalse(stringList.isEmpty());
14 70
     }
15 71
 }