Browse Source

MyArrayList isEmpty()

Trinh Tong 6 years ago
parent
commit
4e149093dc
2 changed files with 43 additions and 5 deletions
  1. 19
    4
      src/main/java/MyArrayList.java
  2. 24
    1
      src/test/java/MyArrayListTest.java

+ 19
- 4
src/main/java/MyArrayList.java View File

18
 
18
 
19
         Object[] tempArray = new Object[getSize() + 1];
19
         Object[] tempArray = new Object[getSize() + 1];
20
         tempArray[size] = data;
20
         tempArray[size] = data;
21
-        this.myArrayList = tempArray;
21
+        myArrayList = tempArray;
22
         setSizeBy1();
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
     public void add(T data, int index) {
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
     public int getSize() {
45
     public int getSize() {
53
         Object[] smallerArray = new Object[this.myArrayList.length - 1];
68
         Object[] smallerArray = new Object[this.myArrayList.length - 1];
54
         int counter = 0;
69
         int counter = 0;
55
         for (int i = 0; i < myArrayList.length; i++) {
70
         for (int i = 0; i < myArrayList.length; i++) {
56
-            System.out.println(myArrayList[i]);
71
+
57
             if (!myArrayList[i].equals(data)) {
72
             if (!myArrayList[i].equals(data)) {
58
                 smallerArray[counter++] = myArrayList[i];
73
                 smallerArray[counter++] = myArrayList[i];
59
             }
74
             }
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
         try {
83
         try {
69
             this.myArrayList[i] = data;
84
             this.myArrayList[i] = data;
70
         } catch (ArrayIndexOutOfBoundsException e) {
85
         } catch (ArrayIndexOutOfBoundsException e) {

+ 24
- 1
src/test/java/MyArrayListTest.java View File

20
     }
20
     }
21
 
21
 
22
     @Test
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
     public void testGet() {
42
     public void testGet() {
24
         String expectedString = "Wilhelm spilled the tea";
43
         String expectedString = "Wilhelm spilled the tea";
25
         Assert.assertTrue(expectedString.equals(stringList.get(stringList.getSize()-1)));
44
         Assert.assertTrue(expectedString.equals(stringList.get(stringList.getSize()-1)));
49
         Assert.assertEquals(expectedString, stringList.get(5));
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
     @Test
76
     @Test
53
     public void testClear() {
77
     public void testClear() {
54
         int expectedSize = 0;
78
         int expectedSize = 0;
61
 
85
 
62
         stringList.clear();
86
         stringList.clear();
63
         Assert.assertEquals(expectedSize, stringList.getSize());
87
         Assert.assertEquals(expectedSize, stringList.getSize());
64
-
65
     }
88
     }
66
 
89
 
67
     @Test
90
     @Test