-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMyArrayList.java
More file actions
179 lines (152 loc) · 4.67 KB
/
MyArrayList.java
File metadata and controls
179 lines (152 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
public class MyArrayList<E> {
private static final Logger logger = Logger.getGlobal();
private E[] myArrayList;
// where size = number of elements in array; size != array's length
private int size = 0;
public MyArrayList() {
this.myArrayList = (E[]) new Object[10];
}
public MyArrayList(Collection<? extends E> collection) {
this.myArrayList = (E[]) collection.toArray();
if (myArrayList.length == 0) {
size = 0;
} else {
for (int i = 0; i < myArrayList.length; i++) {
if (myArrayList[i] == null) {
size = i;
break;
}
}
}
}
public MyArrayList(int initialCapacity) throws IllegalArgumentException {
if (initialCapacity < 0) {
throw new IllegalArgumentException("The array's initial capacity cannot be less than zero.");
} else {
this.myArrayList = (E[]) new Object[initialCapacity];
}
}
public E[] getMyArrayList() {
return myArrayList;
}
public boolean add(E element) {
ensureCapacity();
myArrayList[size] = element;
size++;
return true;
}
public void add(int index, E element) {
if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
ensureCapacity();
if (myArrayList.length > 1) {
shiftElementsFromIndexToSize(index);
}
myArrayList[index] = element;
size++;
}
public E get(int index){
return myArrayList[index];
}
public E remove(int index) {
if (index < 0 || index >= myArrayList.length) {
if (capacity() == 0) {
logger.info("Array length is zero. There are no elements to delete in this array.");
} else {
logger.info("Index out of bounds: " + index);
}
throw new ArrayIndexOutOfBoundsException(index);
}
E elementRemoved = myArrayList[index];
if (! (index == size - 1)) {
shiftElementsFromSizeToIndex(index);
}
myArrayList[size - 1] = null;
size--;
return elementRemoved;
}
public void remove(E element) {
this.remove(this.indexOf(element)) ;
}
public E set(int index, E element) {
E elementPreviouslyAtIndex = myArrayList[index];
if (index < 0 || index >= myArrayList.length) {
throw new ArrayIndexOutOfBoundsException(index);
}
myArrayList[index] = element;
return elementPreviouslyAtIndex;
}
public void clear() {
this.myArrayList = (E[]) new Object[0];
this.size = 0;
}
public boolean isEmpty() {
if (myArrayList.length == 0) {
return true;
} else {
return false;
}
}
public boolean contains(E elementToFind) {
if (isEmpty()) {
return false;
}
for (E elementInList : myArrayList) {
if (elementInList == elementToFind) {
return true;
}
}
return false;
}
public int capacity() {
return myArrayList.length;
}
public int size() {
return size;
}
public void ensureCapacity() {
if (myArrayList.length == 0) {
myArrayList = (E[]) new Object[1];
} else if (size == myArrayList.length) {
doubleArrayCapacity();
}
}
public void doubleArrayCapacity() {
E[] arrayWithDoubleCapacity = (E[]) new Object[size * 2];
System.arraycopy(myArrayList, 0, arrayWithDoubleCapacity, 0, size);
myArrayList = arrayWithDoubleCapacity;
}
public void shiftElementsFromIndexToSize(int index) {
for (int i = size; i > index; i--) {
myArrayList[i] = myArrayList[i - 1];
}
}
public void shiftElementsFromSizeToIndex(int index) {
for (int i = index; i < size - 1; i++) {
myArrayList[i] = myArrayList[i + 1];
}
}
public Object[] toArray() {
return Arrays.copyOf(myArrayList, size);
}
public int indexOf(E element) {
if (element == null) {
for (int i = 0; i < this.myArrayList.length; i++) {
if (this.myArrayList[i] == element) {
return i;
}
}
}
for (int i = 0; i < this.myArrayList.length; i++) {
if (this.myArrayList[i].equals(element)) {
return i;
}
}
return -1;
}
}