|
@@ -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
|
}
|