-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
308 lines (272 loc) · 7.15 KB
/
ArrayList.java
File metadata and controls
308 lines (272 loc) · 7.15 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* @author Giulio Vario
* gvario@mtu.edu
* CS2321
*/
package cs2321;
import java.util.Iterator;
import net.datastructures.List;
public class ArrayList<E> implements List<E> {
private E[] data;
private int size;
private int capacity;
public ArrayList() {
capacity = 16;
data = (E[]) new Object[capacity];
size = 0;
}
/**
* Returns the number of elements in the list.
*
* @return number of elements in the list
*/
@TimeComplexity("O(1)")
@Override
public int size() {
/*
* TCJ
* The method just returns an int variable, which is a primitive operation. This is constant time.
*/
return size;
}
/**
* Tests whether the list is empty.
*
* @return true if the list is empty, false otherwise
*/
@TimeComplexity("O(1)")
@Override
public boolean isEmpty() {
/*
* TCJ
* The method returns a comparison, which is a primitive operation. This is constant time
*/
return size == 0;
}
/**
* Returns (but does not remove) the element at index i.
*
* @param i
* the index of the element to return
* @return the element at the specified index
* @throws IndexOutOfBoundsException
* if the index is negative or greater than size()-1
*/
@TimeComplexity("O(1)")
@Override
public E get(int i) throws IndexOutOfBoundsException {
/*
* TCJ
* Method accesses an array and returns the element in that spot. This is a primitive operation, therefore constant time
*/
if (i < 0 || i > size) {
throw new IndexOutOfBoundsException();
}
return data[i];
}
/**
* Replaces the element at the specified index, and returns the element
* previously stored.
*
* @param i
* the index of the element to replace
* @param e
* the new element to be stored
* @return the previously stored element
* @throws IndexOutOfBoundsException
* if the index is negative or greater than size()-1
*/
@TimeComplexity("O(1)")
@Override
public E set(int i, E e) throws IndexOutOfBoundsException {
/*
* TCJ
* The method accesses data in an array and puts data in, which are both primitive
* operations. This means it is constant time and O(1)
*/
// checks if index inputed is a valid index
if (i < 0 || i > size) {
throw new IndexOutOfBoundsException();
}
E currElement = data[i]; //grab the current element
data[i] = e; //replace the current element in index i with new element
return currElement;
}
/**
* Inserts the given element at the specified index of the list, shifting all
* subsequent elements in the list one position further to make room.
*
* @param i
* the index at which the new element should be stored
* @param e
* the new element to be stored
* @throws IndexOutOfBoundsException
* if the index is negative or greater than size()
*/
@TimeComplexity("O(n)")
@Override
public void add(int i, E e) throws IndexOutOfBoundsException {
/*
* TCJ
* The elements get shifted over when a new one is being added
* The worst case is that n elements are shifted over when i = 0
* This makes the worst case of adding O(n)
*/
// checks if index inputed is a valid index
if (i < 0 || i > size) {
throw new IndexOutOfBoundsException();
}
// doubles array if trying to add past capacity
if (size == capacity) {
int newCapacity = capacity * 2;
E[] newArray = (E[]) new Object[newCapacity];
for (int k = 0; k < data.length; k++) {
newArray[k] = data[k];
}
data = newArray;
capacity = newCapacity;
}
// shifts elements over from the index where you add a new element
for (int j = size - 1; j >= i; j--) {
data[j + 1] = data[j];
}
// add a new element
data[i] = e;
// increase the size of the list
size++;
}
/**
* Removes and returns the element at the given index, shifting all subsequent
* elements in the list one position closer to the front.
*
* @param i
* the index of the element to be removed
* @return the element that had be stored at the given index
* @throws IndexOutOfBoundsException
* if the index is negative or greater than size()
*/
@TimeComplexity("O(n)")
@Override
public E remove(int i) throws IndexOutOfBoundsException {
/*
* TCJ
* all of the elements from i + 1 to the last one shift to the left
* When i = 0, the worst case, there are n shifts so the worst case is
* O(n).
*/
// checks if index inputed is a valid index
if (i < 0 || i >= size) {
throw new IndexOutOfBoundsException();
}
E objectRemoved = data[i]; //grabs data that is being removed
//overwrite the data and shrink the list
for (int j = i; j < size - 1; j++) {
data[i] = data[i + 1];
}
//reflect the new size by decreasing size
size--;
return objectRemoved;
}
/**
* Returns an iterator of the elements stored in the list.
*
* @return iterator of the list's elements
*/
@TimeComplexity("O(1)")
@Override
public Iterator<E> iterator() {
/*
* TCJ
* This method returns a new iterator. This is a primitive operation that
* makes the best and worst case O(1).
*/
return new myIterator();
}
/**
* Class that makes an iterator of the elements stored in the list *
*/
private class myIterator implements Iterator<E> {
int cursor = 0;
/**
* Checks if there is a next element
*
* @return true if there is another element, otherwise false
*/
@Override
public boolean hasNext() {
return cursor < size;
}
/**
* Grabs what the next element in the list is
*
* @return next element in the list
*/
@Override
public E next() {
E e = (E) data[cursor];
cursor++; //move cursor to the next spot
return e;
}
}
/**
* Adds an element to the start of the list
*
* @param e element to be added
* @throws IndexOutOfBoundsException
*/
@TimeComplexity("O(n)")
public void addFirst(E e) throws IndexOutOfBoundsException {
/*
* TCJ
* This is the worst case for the add method causing
* n elements to be shifted over making it O(n)
*/
this.add(0, e);
}
/**
* Adds an element to the end of the list
*
* @param e element to be added
* @throws IndexOutOfBoundsException
*/
@TimeComplexity("O(1)")
public void addLast(E e) throws IndexOutOfBoundsException {
/*
* TCJ
* Adds an element to a free spot in the array, which is a primitive operation
* making this constant time.
*/
this.add(size, e);
}
/**
* Removes the element in the first spot of the list
*
* @return element removed
* @throws IndexOutOfBoundsException
*/
@TimeComplexity("O(n)")
public E removeFirst() throws IndexOutOfBoundsException {
/*
* TCJ
* This causes n elements to be shifted to the left making it the worst case
* of O(n)
*/
return this.remove(0);
}
/**
* Removes an element from the end of the list
*
* @return element removed
* @throws IndexOutOfBoundsException
*/
@TimeComplexity("O(1)")
public E removeLast() throws IndexOutOfBoundsException {
/*
* TCJ
* This is the best case for the remove. Getting rid of an element
* at the end causes no elements to be shifted making this constant time
* and an O(1)
*/
return this.remove(size - 1);
}
}