ソースを参照

working on myarraylist

Jennifer Chao 6 年 前
コミット
c4440e1c80
共有2 個のファイルを変更した356 個の追加1 個の削除を含む
  1. 126
    1
      src/main/java/MyArrayList.java
  2. 230
    0
      src/test/java/MyArrayListTest.java

+ 126
- 1
src/main/java/MyArrayList.java ファイルの表示

@@ -1,2 +1,127 @@
1
-public class MyArrayList {
1
+import java.lang.reflect.Array;
2
+
3
+public class MyArrayList<T> {
4
+
5
+    private T[] originalArray;
6
+    private int size;
7
+
8
+    public MyArrayList() {
9
+        this.size = 0;
10
+        this.originalArray = (T[]) new Object[0];
11
+    }
12
+
13
+    public MyArrayList(int size) {
14
+        this.size = size;
15
+        this.originalArray = (T[]) new Object[size];
16
+    }
17
+
18
+
19
+    public void add(T value) {
20
+        T[] newArray = (T[]) Array.newInstance(this.originalArray.getClass().getComponentType(), originalArray.length + 1);
21
+        // T[] newArray = (T[]) new Object[originalArray.length + 1];
22
+
23
+        for (int i = 0; i < originalArray.length; i++) {
24
+            newArray[i] = originalArray[i];
25
+        }
26
+
27
+        newArray[newArray.length - 1] = value;
28
+        setOriginalArray(newArray);
29
+        setSize(newArray.length);
30
+    }
31
+
32
+    public void add(T value, int index) {
33
+        T[] newArray = (T[]) Array.newInstance(this.originalArray.getClass().getComponentType(), originalArray.length + 1);
34
+
35
+        for (int i = 0; i < newArray.length; i++) {
36
+            if (i < index) {
37
+                newArray[i] = originalArray[i];
38
+            } else if (i == index) {
39
+                newArray[i] = value;
40
+            } else if (i > index) {
41
+                newArray[i] = originalArray[i - 1];
42
+            }
43
+        }
44
+
45
+        setOriginalArray(newArray);
46
+        setSize(newArray.length);
47
+    }
48
+
49
+    public T get(int index) {
50
+        return originalArray[index];
51
+    }
52
+
53
+    public void remove(T value) {
54
+        T[] newArray = (T[]) Array.newInstance(this.originalArray.getClass().getComponentType(), originalArray.length - 1);
55
+        int indexToRemove = 0;
56
+
57
+        for (int i = 0; i < originalArray.length; i++) {
58
+            if (originalArray[i] == value) {
59
+                indexToRemove = i;
60
+            }
61
+        }
62
+
63
+        for (int i = 0; i < originalArray.length; i++) {
64
+            if (i < indexToRemove) {
65
+                newArray[i] = originalArray[i];
66
+            } else if (i > indexToRemove) {
67
+                newArray[i - 1] = originalArray[i];
68
+            }
69
+        }
70
+
71
+        setOriginalArray(newArray);
72
+        setSize(newArray.length);
73
+    }
74
+
75
+    public void set(int index, T value) {
76
+        originalArray[index] = value;
77
+    }
78
+
79
+    public void clear() {
80
+        setOriginalArray((T[]) Array.newInstance(this.originalArray.getClass().getComponentType(), 0));
81
+        setSize(0);
82
+    }
83
+
84
+    public boolean isEmpty() {
85
+        if (this.size() == 0) {
86
+            return true;
87
+        }
88
+        return false;
89
+    }
90
+
91
+    public boolean contains(T value) {
92
+        for (int i = 0; i < originalArray.length; i++) {
93
+            if (originalArray[i] == value) {
94
+                return true;
95
+            }
96
+        }
97
+        return false;
98
+    }
99
+
100
+    public int indexOf(T value) {
101
+        return 0;
102
+    }
103
+
104
+    public int lastIndexOf(T value) {
105
+        return 0; // -1 if doesnt contain
106
+    }
107
+
108
+    public int size() {
109
+        return size;
110
+    }
111
+
112
+    public T[] toArray() {
113
+        return null;
114
+    }
115
+
116
+    public T[] getOriginalArray() {
117
+        return originalArray;
118
+    }
119
+
120
+    public void setOriginalArray(T[] originalArray) {
121
+        this.originalArray = originalArray;
122
+    }
123
+
124
+    public void setSize(int size) {
125
+        this.size = size;
126
+    }
2 127
 }

+ 230
- 0
src/test/java/MyArrayListTest.java ファイルの表示

@@ -1,2 +1,232 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import java.lang.reflect.Array;
5
+import java.util.ArrayList;
6
+
1 7
 public class MyArrayListTest {
8
+
9
+    MyArrayList<Integer> testArray = new MyArrayList<>();
10
+
11
+    public void populateArray() {
12
+        testArray.add(10);
13
+        testArray.add(3);
14
+        testArray.add(9);
15
+        testArray.add(2);
16
+    }
17
+
18
+
19
+    @Test
20
+    public void test(){
21
+        Integer[] i = new Integer[0];
22
+        System.out.println(i.getClass());
23
+
24
+        Object[] objects = new Object[0];
25
+        System.out.println(objects.getClass());
26
+
27
+        populateArray();
28
+        System.out.println(testArray.get(0).getClass());
29
+
30
+        MyArrayList<String> testStringArray = new MyArrayList<>();
31
+        testStringArray.add("Meow");
32
+        System.out.println(testStringArray.get(0).getClass());
33
+        // System.out.println(testStringArray.getOriginalArray()[0]);
34
+    }
35
+
36
+    @Test
37
+    public void initiateSizeOnConstruction() {
38
+        MyArrayList<Integer> testArray1 = new MyArrayList<>(5);
39
+
40
+        int expected = 5;
41
+        int actual = testArray1.size();
42
+
43
+        Assert.assertEquals(expected, actual);
44
+    }
45
+
46
+    @Test
47
+    public void testAdd() {
48
+        testArray.add(5);
49
+
50
+        int expected = 5;
51
+        int actual = testArray.get(0);
52
+
53
+        Assert.assertEquals(expected, actual);
54
+    }
55
+
56
+    @Test
57
+    public void testAdd_Size() {
58
+        testArray.add(5);
59
+
60
+        int expected = 1;
61
+        int actual = testArray.size();
62
+
63
+        Assert.assertEquals(expected, actual);
64
+    }
65
+
66
+    @Test
67
+    public void testAdd_Multiple() {
68
+        populateArray();
69
+
70
+        int expected = 9;
71
+        int actual = testArray.get(2);
72
+
73
+        Assert.assertEquals(expected, actual);
74
+    }
75
+
76
+    @Test
77
+    public void testAdd_Multiple_String() {
78
+        MyArrayList<String> testStringArray = new MyArrayList<>();
79
+        testStringArray.add("Jenn");
80
+        testStringArray.add("Ned");
81
+
82
+        String expected = "Ned";
83
+        String actual = testStringArray.get(1);
84
+
85
+        Assert.assertEquals(expected, actual);
86
+    }
87
+
88
+    @Test
89
+    public void testAdd_Multiple_String_Size() {
90
+        MyArrayList<String> testStringArray = new MyArrayList<>();
91
+        testStringArray.add("Jenn");
92
+        testStringArray.add("Ned");
93
+        testStringArray.add("Thulasi");
94
+
95
+        int expected = 3;
96
+        int actual = testStringArray.size();
97
+
98
+        Assert.assertEquals(expected, actual);
99
+    }
100
+
101
+    @Test
102
+    public void testAdd_Multiple_Size() {
103
+        populateArray();
104
+
105
+        int expected = 4;
106
+        int actual = testArray.size();
107
+
108
+        Assert.assertEquals(expected, actual);
109
+    }
110
+
111
+    @Test
112
+    public void testAdd_AtIndex() {
113
+        populateArray();
114
+        testArray.add(5, 2);
115
+
116
+        int expected = 5;
117
+        int actual = testArray.get(2);
118
+
119
+        Assert.assertEquals(expected, actual);
120
+    }
121
+
122
+    @Test
123
+    public void testAdd_AtIndex_Size() {
124
+        populateArray();
125
+        testArray.add(5, 2);
126
+
127
+        int expected = 5;
128
+        int actual = testArray.size();
129
+
130
+        Assert.assertEquals(expected, actual);
131
+    }
132
+
133
+    @Test
134
+    public void testRemove_First() {
135
+        populateArray();
136
+        testArray.remove(10);
137
+
138
+        int unexpected = 10;
139
+        int actual = testArray.get(0);
140
+
141
+        Assert.assertNotEquals(unexpected, actual);
142
+    }
143
+
144
+    @Test
145
+    public void testRemove_Middle() {
146
+        populateArray();
147
+        testArray.remove(9);
148
+
149
+        int expected = 2;
150
+        int actual = testArray.get(2);
151
+
152
+        Assert.assertEquals(expected, actual);
153
+    }
154
+
155
+    @Test
156
+    public void testRemove_Size() {
157
+        populateArray();
158
+        testArray.remove(2);
159
+
160
+        int expected = 3;
161
+        int actual = testArray.size();
162
+
163
+        Assert.assertEquals(expected, actual);
164
+    }
165
+
166
+    @Test
167
+    public void testIsEmpty_True() {
168
+        Assert.assertTrue(testArray.isEmpty());
169
+    }
170
+
171
+    @Test
172
+    public void testIsEmpty_False() {
173
+        populateArray();
174
+        Assert.assertFalse(testArray.isEmpty());
175
+    }
176
+
177
+    @Test
178
+    public void testContains_True() {
179
+        populateArray();
180
+        Assert.assertTrue(testArray.contains(9));
181
+    }
182
+
183
+    @Test
184
+    public void testContains_False() {
185
+        populateArray();
186
+        Assert.assertFalse(testArray.contains(5));
187
+    }
188
+
189
+    @Test
190
+    public void testSet() {
191
+        populateArray();
192
+        testArray.set(2, 55);
193
+
194
+        int expected = 55;
195
+        int actual = testArray.get(2);
196
+
197
+        Assert.assertEquals(expected, actual);
198
+    }
199
+
200
+    @Test
201
+    public void testSet_Again() {
202
+        populateArray();
203
+
204
+        testArray.set(0, 11);
205
+
206
+        int expected = 11;
207
+        int actual = testArray.get(0);
208
+
209
+        Assert.assertEquals(expected, actual);
210
+    }
211
+
212
+    @Test(expected = IndexOutOfBoundsException.class)
213
+    public void testClear() {
214
+        populateArray();
215
+        testArray.clear();
216
+
217
+        testArray.get(0);
218
+    }
219
+
220
+    @Test
221
+    public void testClear_Size() {
222
+        populateArray();
223
+        testArray.clear();
224
+
225
+        int expected = 0;
226
+        int actual = testArray.size();
227
+
228
+        Assert.assertEquals(expected, actual);
229
+    }
230
+
231
+
2 232
 }