-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
executable file
·278 lines (247 loc) · 6.55 KB
/
Copy pathArrayList.java
File metadata and controls
executable file
·278 lines (247 loc) · 6.55 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.util.Arrays;
public class ArrayList<E> {
/**
* the array which stores the elements of the ArrayList
*/
private E[] list;
/**
* the default capacity of the ArrayList
*/
private final int DEFAULT_CAP = 10;
/**
* the current capacity of the ArrayList - equivalent to the length of list
*/
private int capacity;
/**
* the number of elements in ArrayList
*/
private int size;
/**
* Constructs an ArrayList of default capacity 10
*/
@SuppressWarnings("unchecked")
public ArrayList() {
list = (E[])new Object[DEFAULT_CAP];
capacity = 10;
size = 0;
}
/**
* Constructs an ArrayList with a capacity of num
* @param num the capacity of the ArrayList to be constructed
*/
@SuppressWarnings("unchecked")
public ArrayList(int initialCap) {
list = (E[])new Object[0];
capacity = initialCap;
size = 0;
}
/**
* Constructs an ArrayList with the elements of the ArrayList passed as parameter - shallow clone
* @param lst the ArrayList that has the elements the new ArrayList will contain
*/
@SuppressWarnings("unchecked")
public ArrayList(ArrayList<E> lst) {
list = (E[])new Object[lst.size()];
capacity = lst.capacity;
size = lst.size;
for(int i = 0; i < size; i++) {
list[i] = lst.list[i];
}
}
/**
* Appends an element to the end of the ArrayList
* @param e the element to append at the end of the ArrayList
* @return true if the element was successfully appended
*/
public void add(E e) {
resize();
list[size] = e;
size++;
}
/**
* Resizes the ArrayList if the number of elements it contains is equal
* to its capacity
*/
@SuppressWarnings("unchecked")
private void resize() {
int newCap = Math.max(capacity*2, DEFAULT_CAP);
if(size < list.length) return;
Object[] temp = new Object[newCap];
for(int i = 0; i < list.length; i++) {
temp[i] = list[i];
}
list = (E[])temp;
capacity = newCap;
}
/**
* Inserts an element at the specified index, pushes the current
* elements at and after the insertion point back
* @param index the index where the element will be inserted into
* the ArrayList
* @param e the element to be inserted
*/
public void add(int index, E e) {
resize();
for(int i = size-1; i >= index; i--) {
list[i+1] = list[i];
}
list[index] = e;
size++;
}
/**
* Creates a new, empty ArrayList of the same capacity it had
* previously
*/
@SuppressWarnings("unchecked")
public void clear() {
list = (E[])new Object[capacity];
size = 0;
}
/**
* Clones the current ArrayList and returns it
*/
public ArrayList<E> clone() {
ArrayList<E> temp = new ArrayList<E>();
temp.size = this.size;
temp.capacity = this.capacity;
temp.list = Arrays.copyOf(this.list, this.list.length);
return temp;
}
/**
* Checks if the ArrayList contains an object
* @param o the object to check
* @return true if the ArrayList contains o, false otherwise
*/
public boolean contains(Object o) {
for(int i = 0; i < size; i++) {
if(list[i].equals(o)) return true;
}
return false;
}
/**
* Returns the element at a specified index of the ArrayList
* @param index the index of the element to return
* @return the element at index
*/
public E get(int index) {
return (E)list[index];
}
/**
* Finds the index of an Object in the ArrayList
* @param o the object
* @return the index of o if it is in the ArrayList, -1 if it
* is not in the ArrayList
*/
public int indexOf(Object o) {
int index = -1;
for(int i = 0; i < size; i++) {
if(list[i].equals(o)) {
index = i; break;
}
}
return index;
}
/**
* Checks if the ArrayList is empty
* @return true if the ArrayList contains no elements,
* false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Finds the last index of object o
* @param o the object
* @return the last index of o in the ArrayList, -1 if
* it is not in the ArrayList
*/
public int lastIndexOf(Object o) {
int index = -1;
for(int i = 0; i < size; i++) {
if(list[i].equals(o)) index = i;
}
return index;
}
/**
* Removes the element at the specified index, shifts the
* following elements accordingly
* @param index to remove
* @return the element that is removed
*/
public E remove(int index) {
if(index > size) return null;
E element = null;
for(int i = index+1; i < size; i++) {
list[i-1] = list[i];
}
list[size-1] = null;
size--;
return element;
}
/**
* Removes the specified object from the ArrayList
* @param o the object to remove
* @return true if the Object is successfully, false otherwise
*/
public boolean remove(Object o) {
int index = indexOf(o);
if (index == -1) return false;
remove(index);
return true;
}
/**
* Sets the element at the specified index to the given element
* @param index the index to set the new element
* @param element the new element
* @return the element that was previously at the given index
*/
public E set(int index, E element) {
if(index < 0 || index >= size())
throw new IndexOutOfBoundsException();
E currentElement = get(index);
list[index] = element;
return currentElement;
}
/**
* Returns the number of elements in the ArrayList
* @return size - how many elements the ArrayList contains
*/
public int size() {
return size;
}
/**
* Returns a sublist of the current ArrayList
* @param fromIndex the beginning of the subList
* @param toIndex the end of the subList
* @return a new ArrayList with a shallow copy of the elements
* from the index fromIndex to the index toIndex
*/
public ArrayList<E> subList(int fromIndex, int toIndex) {
ArrayList<E> temp = new ArrayList<E>();
for(int i = fromIndex; i< toIndex; i++) {
temp.add(this.get(i));
}
return temp;
}
/**
* Returns the elements of the ArrayList in an array
* @return an array containing a shallow copy of the elements
* in ArrayList
*/
public Object[] toArray() {
Object[] arr = new Object[size];
for(int i = 0; i < this.size; i++) {
arr[i] = this.get(i);
}
return arr;
}
public String toString() {
if(size == 0) return "[]";
String str = "{" + list[0];
for(int i = 1; i < size; i++) {
str = str + ", " + list[i];
}
str = str + "]";
return str;
}
}