Trinh Tong 6 лет назад
Родитель
Сommit
1c58ba9654
2 измененных файлов: 35 добавлений и 22 удалений
  1. 28
    14
      src/main/java/MyArrayList.java
  2. 7
    8
      src/test/java/MyArrayListTest.java

+ 28
- 14
src/main/java/MyArrayList.java Просмотреть файл

@@ -1,3 +1,6 @@
1
+import java.util.ArrayList;
2
+import java.util.Arrays;
3
+
1 4
 public class MyArrayList<T> {
2 5
     private final static int INITIAL_SIZE = 0;
3 6
     private Object[] myArrayList;
@@ -13,12 +16,12 @@ public class MyArrayList<T> {
13 16
         this.size = size;
14 17
     }
15 18
 
16
-
17 19
     public void add(T data) {
18 20
 
19
-        Object[] tempArray = new Object[getSize() + 1];
20
-        tempArray[size] = data;
21
-        myArrayList = tempArray;
21
+        Object[] tempArray1 = new Object[getSize() + 1];
22
+        copy(tempArray1, myArrayList);
23
+        tempArray1[getSize()] = data;
24
+        myArrayList = tempArray1;
22 25
         setSizeBy1();
23 26
 
24 27
     }
@@ -29,17 +32,24 @@ public class MyArrayList<T> {
29 32
         }
30 33
     }
31 34
 
32
-    public void add(T data, int index) {
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!");
35
+    public void add(int index, T data) {
36
+        Object[] newArray = new Object[getSize()];
37
+        Object[] firstArray = Arrays.copyOfRange(myArrayList, 0, index);
38
+        for (int i = 0; i < firstArray.length; i++) {
39
+            newArray[i] = firstArray[i];
40
+        }
41
+
42
+        Object[] secondArray = Arrays.copyOfRange(myArrayList, index, getSize());
43
+        int count = 1;
44
+        newArray[index] = data;
45
+        for (int i = index + 1; i < newArray.length; i++) {
46
+
47
+            newArray[i] = secondArray[count++];
42 48
         }
49
+
50
+        setSize(newArray.length);
51
+        myArrayList = newArray;
52
+
43 53
     }
44 54
 
45 55
     public int getSize() {
@@ -50,6 +60,10 @@ public class MyArrayList<T> {
50 60
         this.size++;
51 61
     }
52 62
 
63
+    public void setSize(int i) {
64
+        this.size = i;
65
+    }
66
+
53 67
     public void resetSize() {
54 68
         this.size = INITIAL_SIZE;
55 69
     }

+ 7
- 8
src/test/java/MyArrayListTest.java Просмотреть файл

@@ -28,11 +28,15 @@ public class MyArrayListTest {
28 28
         stringList.add("Wilhelm spilled the tea");
29 29
         stringList.add("the table got wet :(");
30 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 31
 
34 32
 
35
-        stringList.add("Dolio Rollio", 1);
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));
36 40
 
37 41
         Assert.assertTrue(expectedString.equals(stringList.get(1)));
38 42
 
@@ -68,11 +72,6 @@ public class MyArrayListTest {
68 72
         Assert.assertEquals(expectedString, stringList.get(5));
69 73
     }
70 74
 
71
-//    @Test(expected = ArrayIndexOutOfBoundsException.class)
72
-//    public void testSetException() throws ArrayIndexOutOfBoundsException {
73
-//        stringList.set(100, "Froilon's fav color is red");
74
-//    }
75
-
76 75
     @Test
77 76
     public void testClear() {
78 77
         int expectedSize = 0;