瀏覽代碼

MyArrayList isEmpty()

Trinh Tong 6 年之前
父節點
當前提交
4e149093dc
共有 2 個檔案被更改,包括 43 行新增5 行删除
  1. 19
    4
      src/main/java/MyArrayList.java
  2. 24
    1
      src/test/java/MyArrayListTest.java

+ 19
- 4
src/main/java/MyArrayList.java 查看文件

@@ -18,13 +18,28 @@ public class MyArrayList<T> {
18 18
 
19 19
         Object[] tempArray = new Object[getSize() + 1];
20 20
         tempArray[size] = data;
21
-        this.myArrayList = tempArray;
21
+        myArrayList = tempArray;
22 22
         setSizeBy1();
23 23
 
24 24
     }
25 25
 
26
+    public void copy(Object[] newArr, Object[] src) {
27
+        for (int i = 0; i < src.length; i++) {
28
+            newArr[i] = src[i];
29
+        }
30
+    }
31
+
26 32
     public void add(T data, int index) {
27
-        
33
+        try {
34
+            Object[] tempoArray = new Object[getSize() + 1];
35
+            copy(tempoArray, myArrayList);
36
+            setSizeBy1();
37
+            myArrayList = tempoArray;
38
+            add(data);
39
+            set(index, data);
40
+        } catch (ArrayIndexOutOfBoundsException e) {
41
+            System.out.println("That index is not in my array list!");
42
+        }
28 43
     }
29 44
 
30 45
     public int getSize() {
@@ -53,7 +68,7 @@ public class MyArrayList<T> {
53 68
         Object[] smallerArray = new Object[this.myArrayList.length - 1];
54 69
         int counter = 0;
55 70
         for (int i = 0; i < myArrayList.length; i++) {
56
-            System.out.println(myArrayList[i]);
71
+
57 72
             if (!myArrayList[i].equals(data)) {
58 73
                 smallerArray[counter++] = myArrayList[i];
59 74
             }
@@ -64,7 +79,7 @@ public class MyArrayList<T> {
64 79
     }
65 80
 
66 81
 
67
-    public void set(int i, T data) {
82
+    public void set(int i, T data) throws ArrayIndexOutOfBoundsException {
68 83
         try {
69 84
             this.myArrayList[i] = data;
70 85
         } catch (ArrayIndexOutOfBoundsException e) {

+ 24
- 1
src/test/java/MyArrayListTest.java 查看文件

@@ -20,6 +20,25 @@ public class MyArrayListTest {
20 20
     }
21 21
 
22 22
     @Test
23
+    public void testAddatIndex() {
24
+
25
+        String expectedString = "Dolio Rollio";
26
+
27
+        stringList.add("Wilhelm spilled the tea");
28
+        stringList.add("Wilhelm spilled the tea");
29
+        stringList.add("the table got wet :(");
30
+        stringList.add("Nhu got more tears to fill up her tea");
31
+        stringList.add("Wilhelm spilled the tea");
32
+        stringList.add("Wilhelm spilled the tea");
33
+
34
+
35
+        stringList.add("Dolio Rollio", 1);
36
+
37
+        Assert.assertTrue(expectedString.equals(stringList.get(1)));
38
+
39
+    }
40
+
41
+    @Test
23 42
     public void testGet() {
24 43
         String expectedString = "Wilhelm spilled the tea";
25 44
         Assert.assertTrue(expectedString.equals(stringList.get(stringList.getSize()-1)));
@@ -49,6 +68,11 @@ public class MyArrayListTest {
49 68
         Assert.assertEquals(expectedString, stringList.get(5));
50 69
     }
51 70
 
71
+//    @Test(expected = ArrayIndexOutOfBoundsException.class)
72
+//    public void testSetException() throws ArrayIndexOutOfBoundsException {
73
+//        stringList.set(100, "Froilon's fav color is red");
74
+//    }
75
+
52 76
     @Test
53 77
     public void testClear() {
54 78
         int expectedSize = 0;
@@ -61,7 +85,6 @@ public class MyArrayListTest {
61 85
 
62 86
         stringList.clear();
63 87
         Assert.assertEquals(expectedSize, stringList.getSize());
64
-
65 88
     }
66 89
 
67 90
     @Test