- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
Latest commit
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
importjava.util.Arrays;
publicclassArrayList<E> {
/**
* the array which stores the elements of the ArrayList
*/
privateE[] list;
/**
* the default capacity of the ArrayList
*/
privatefinalintDEFAULT_CAP = 10;
/**
* the current capacity of the ArrayList - equivalent to the length of list
*/
privateintcapacity;
/**
* the number of elements in ArrayList
*/
privateintsize;
/**
* Constructs an ArrayList of default capacity 10
*/
@SuppressWarnings("unchecked")
publicArrayList() {
list = (E[])newObject[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")
publicArrayList(intinitialCap) {
list = (E[])newObject[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")
publicArrayList(ArrayList<E> lst) {
list = (E[])newObject[lst.size()];
capacity = lst.capacity;
size = lst.size;
for(inti = 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
*/
publicvoidadd(Ee) {
resize();
list[size] = e;
size++;
}
/**
* Resizes the ArrayList if the number of elements it contains is equal
* to its capacity
*/
@SuppressWarnings("unchecked")
privatevoidresize() {
intnewCap = Math.max(capacity*2, DEFAULT_CAP);
if(size < list.length) return;
Object[] temp = newObject[newCap];
for(inti = 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
*/
publicvoidadd(intindex, Ee) {
resize();
for(inti = 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")
publicvoidclear() {
list = (E[])newObject[capacity];
size = 0;
}
/**
* Clones the current ArrayList and returns it
*/
publicArrayList<E> clone() {
ArrayList<E> temp = newArrayList<E>();
temp.size = this.size;
temp.capacity = this.capacity;
temp.list = Arrays.copyOf(this.list, this.list.length);
returntemp;
}
/**
* Checks if the ArrayList contains an object
* @param o the object to check
* @return true if the ArrayList contains o, false otherwise
*/
publicbooleancontains(Objecto) {
for(inti = 0; i < size; i++) {
if(list[i].equals(o)) returntrue;
}
returnfalse;
}
/**
* Returns the element at a specified index of the ArrayList
* @param index the index of the element to return
* @return the element at index
*/
publicEget(intindex) {
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
*/
publicintindexOf(Objecto) {
intindex = -1;
for(inti = 0; i < size; i++) {
if(list[i].equals(o)) {
index = i; break;
}
}
returnindex;
}
/**
* Checks if the ArrayList is empty
* @return true if the ArrayList contains no elements,
* false otherwise
*/
publicbooleanisEmpty() {
returnsize == 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
*/
publicintlastIndexOf(Objecto) {
intindex = -1;
for(inti = 0; i < size; i++) {
if(list[i].equals(o)) index = i;
}
returnindex;
}
/**
* Removes the element at the specified index, shifts the
* following elements accordingly
* @param index to remove
* @return the element that is removed
*/
publicEremove(intindex) {
if(index > size) returnnull;
Eelement = null;
for(inti = index+1; i < size; i++) {
list[i-1] = list[i];
}
list[size-1] = null;
size--;
returnelement;
}
/**
* Removes the specified object from the ArrayList
* @param o the object to remove
* @return true if the Object is successfully, false otherwise
*/
publicbooleanremove(Objecto) {
intindex = indexOf(o);
if (index == -1) returnfalse;
remove(index);
returntrue;
}
/**
* 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
*/
publicEset(intindex, Eelement) {
if(index < 0 || index >= size())
thrownewIndexOutOfBoundsException();
EcurrentElement = get(index);
list[index] = element;
returncurrentElement;
}
/**
* Returns the number of elements in the ArrayList
* @return size - how many elements the ArrayList contains
*/
publicintsize() {
returnsize;
}
/**
* 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
*/
publicArrayList<E> subList(intfromIndex, inttoIndex) {
ArrayList<E> temp = newArrayList<E>();
for(inti = fromIndex; i< toIndex; i++) {
temp.add(this.get(i));
}
returntemp;
}
/**
* Returns the elements of the ArrayList in an array
* @return an array containing a shallow copy of the elements
* in ArrayList
*/
publicObject[] toArray() {
Object[] arr = newObject[size];
for(inti = 0; i < this.size; i++) {
arr[i] = this.get(i);
}
returnarr;
}
publicStringtoString() {
if(size == 0) return"[]";
Stringstr = "{" + list[0];
for(inti = 1; i < size; i++) {
str = str + ", " + list[i];
}
str = str + "]";
returnstr;
}
}