-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
226 lines (196 loc) · 4.69 KB
/
ArrayList.java
File metadata and controls
226 lines (196 loc) · 4.69 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
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayList<E> implements List<E> {
// private fields
private E elements[];
private int currentSize;
private class ListIterator implements Iterator<E> {
private int currentPosition;
public ListIterator() {
this.currentPosition = 0;
}
@Override
public boolean hasNext() {
return this.currentPosition < size();
}
@Override
public E next() {
if (this.hasNext()) {
return (E) elements[this.currentPosition++];
}
else
throw new NoSuchElementException();
}
}
@SuppressWarnings("unchecked")
public ArrayList(int initialCapacity) {
if (initialCapacity < 1)
throw new IllegalArgumentException("Capacity must be at least 1.");
this.currentSize = 0;
this.elements = (E[]) new Object[initialCapacity];
}
@Override
public void add(E obj) {
if (obj == null)
throw new IllegalArgumentException("Object cannot be null.");
else {
if (this.size() == this.elements.length)
reAllocate();
this.elements[this.currentSize++] = obj;
}
}
@SuppressWarnings("unchecked")
private void reAllocate() {
// create a new array with twice the size
E newElements[] = (E[]) new Object[2*this.elements.length];
for (int i = 0; i < this.size(); i++)
newElements[i] = this.elements[i];
// replace old elements with newElements
this.clear();
this.elements = newElements;
}
@Override
public void add(int index, E obj) {
if (obj == null)
throw new IllegalArgumentException("Object cannot be null.");
if (index == this.currentSize)
this.add(obj); // Use other method to "append"
else {
if (index >= 0 && index < this.currentSize) {
if (this.currentSize == this.elements.length)
reAllocate();
// move everybody one spot to the back
for (int i = this.currentSize; i > index; i--)
this.elements[i] = this.elements[i - 1];
// add element at position index
this.elements[index] = obj;
this.currentSize++;
}
else
throw new ArrayIndexOutOfBoundsException();
}
}
@Override
public boolean remove(E obj) {
if (obj == null)
throw new IllegalArgumentException("Object cannot be null.");
// first find obj in the array
int position = this.firstIndex(obj);
if (position >= 0) // found it
return this.remove(position);
else
return false;
}
@Override
public boolean remove(int index) {
if (index >= 0 && index < this.currentSize) {
// move everybody one spot to the front
for (int i = index; i < this.currentSize - 1; i++)
this.elements[i] = this.elements[i + 1];
this.elements[--this.currentSize] = null;
return true;
}
else
return false;
}
@Override
public int removeAll(E obj) {
int counter = 0;
while (this.remove(obj))
counter++;
return counter;
}
@Override
public E get(int index) {
if (index >= 0 && index < this.size())
return this.elements[index];
else
throw new ArrayIndexOutOfBoundsException();
}
@Override
public E set(int index, E obj) {
if (obj == null)
throw new IllegalArgumentException("Object cannot be null.");
if (index >= 0 && index < this.size()) {
E temp = this.elements[index];
this.elements[index] = obj;
return temp;
}
else
throw new ArrayIndexOutOfBoundsException();
}
@Override
public E first() {
if (this.isEmpty())
return null;
else
return this.elements[0];
}
@Override
public E last() {
if (this.isEmpty())
return null;
else
return this.elements[this.currentSize - 1];
}
@Override
public int firstIndex(E obj) {
for (int i = 0; i < this.size(); i++)
if (this.elements[i].equals(obj))
return i;
return -1;
}
@Override
public int lastIndex(E obj) {
for (int i = this.size() - 1; i >= 0; i--)
if (this.elements[i].equals(obj))
return i;
return -1;
}
@Override
public int size() {
return this.currentSize;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public boolean contains(E obj) {
return this.firstIndex(obj) >= 0;
}
@Override
public void clear() {
for (int i = 0; i < this.currentSize; i++)
this.elements[i] = null;
this.currentSize = 0;
}
@Override
public Iterator<E> iterator() {
return new ListIterator();
}
@Override
public int replaceAll(E e, E f) {
int timesReplaced = 0;
for (int i = 0; i < this.currentSize; i++) {
if(this.get(i) == e) {
this.remove(i);
this.add(i, f);
++timesReplaced;
}
}
return timesReplaced;
}
@Override
public List<E> reverse() {
int size = this.currentSize;
List<E> reversed = new ArrayList<E>(currentSize);
if(this.isEmpty() == false) {
for (int i = 0; i < this.currentSize; i++) {
reversed.add(i, this.get(size));
--size;
}
}
return reversed;
}
}