|
@@ -1,21 +1,82 @@
|
1
|
|
-import java.lang.reflect.Array;
|
2
|
|
-
|
3
|
1
|
public class MyArrayList<T> {
|
4
|
|
- private final int INITIAL_SIZE = 10;
|
|
2
|
+ private final static int INITIAL_SIZE = 0;
|
5
|
3
|
private Object[] myArrayList;
|
6
|
4
|
private int size;
|
7
|
5
|
|
8
|
|
- // create array of Object[3]
|
9
|
|
-
|
10
|
6
|
public MyArrayList() {
|
11
|
|
- this.myArrayList = new Object[INITIAL_SIZE];
|
12
|
|
- size = INITIAL_SIZE;
|
|
7
|
+ this(INITIAL_SIZE);
|
|
8
|
+ this.size = myArrayList.length;
|
|
9
|
+ }
|
|
10
|
+
|
|
11
|
+ public MyArrayList(int size) {
|
|
12
|
+ this.myArrayList = new Object[size];
|
|
13
|
+ this.size = size;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+ public void add(T data) {
|
|
18
|
+
|
|
19
|
+ Object[] tempArray = new Object[getSize() + 1];
|
|
20
|
+ tempArray[size] = data;
|
|
21
|
+ this.myArrayList = tempArray;
|
|
22
|
+ setSizeBy1();
|
|
23
|
+
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ public int getSize() {
|
|
27
|
+ return this.size;
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ public void setSizeBy1() {
|
|
31
|
+ this.size++;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ public void resetSize() {
|
|
35
|
+ this.size = INITIAL_SIZE;
|
13
|
36
|
}
|
14
|
37
|
|
15
|
|
- public MyArrayList(T[] myArrayList) {
|
16
|
|
- this.myArrayList = myArrayList;
|
17
|
|
- size = myArrayList.length;
|
|
38
|
+ public void decreaseSize() {
|
|
39
|
+ this.size--;
|
18
|
40
|
}
|
19
|
41
|
|
20
|
|
-
|
|
42
|
+ public T get(int i) {
|
|
43
|
+ return (T) this.myArrayList[i];
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public void remove(T data) {
|
|
47
|
+ // removes first instance of that data
|
|
48
|
+
|
|
49
|
+ Object[] smallerArray = new Object[this.myArrayList.length - 1];
|
|
50
|
+ int counter = 0;
|
|
51
|
+ for (int i = 0; i < myArrayList.length; i++) {
|
|
52
|
+ System.out.println(myArrayList[i]);
|
|
53
|
+ if (!myArrayList[i].equals(data)) {
|
|
54
|
+ smallerArray[counter++] = myArrayList[i];
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ this.myArrayList = smallerArray;
|
|
59
|
+ decreaseSize();
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+ public void set(int i, T data) {
|
|
64
|
+ try {
|
|
65
|
+ this.myArrayList[i] = data;
|
|
66
|
+ } catch (ArrayIndexOutOfBoundsException e) {
|
|
67
|
+ System.out.println("That index is not in my array list!");
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+ public void clear() {
|
|
73
|
+ Object[] tempArr = new Object[INITIAL_SIZE];
|
|
74
|
+ resetSize();
|
|
75
|
+
|
|
76
|
+ myArrayList = tempArr;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public boolean isEmpty() {
|
|
80
|
+ return (getSize() == 0);
|
|
81
|
+ }
|
21
|
82
|
}
|