-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArrayList.java
More file actions
221 lines (201 loc) · 5.83 KB
/
Copy pathMyArrayList.java
File metadata and controls
221 lines (201 loc) · 5.83 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
package IntensiveCourse.Lesson1;
import java.util.Comparator;
/**
* Personal implementation of the ArrayList class. Not thread safe.
*
* @author DRMPN
*
*/
public class MyArrayList<E> implements MyList<E> {
private int size = 0;
private Object[] elementData;
private final int DEFAULT_CAPACITY = 10;
/**
* Creates a new MyArrayList with default capacity.
*
*/
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
/**
* Creates a new MyArrayList with a given capacity.
*
* @param capacity - the size of a newly created list
*
*/
public MyArrayList(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Illegal Capacity: " + capacity);
}
elementData = new Object[capacity];
}
/**
* Appends the given element to the end of this list.
*
* @param element - element to be appended to this list
* @return true
*/
@Override
public boolean add(E element) {
if (ensureCapacity(size)) {
increaseCapacity(size);
}
elementData[size] = element;
size++;
return true;
}
/**
* Inserts the given element to the given index of this list.
*
* @param index - index of the element to add
* @param element - element to be appended to this list
*/
@Override
public void add(int index, E element) {
if (ensureCapacity(size)) {
increaseCapacity(size);
}
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size++;
}
/**
* Removes all of the elements from this list.
*/
@Override
public void clear() {
for (int i = 0; i < size; i++) {
elementData[i] = null;
}
size = 0;
}
/**
* Returns the element at the specified position in this list.
*
* @param index - index of the element to return
* @return Returns the element at the specified position in this list
*/
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return (E) elementData[index];
}
/**
* Returns the index of the first occurrence of the specified element in this
* list,
* or -1 if this list does not contain the element.
*
* @param o - element to search for
* @return the index of the first occurrence of the specified element in this
* list,
* or -1 if this list does not contain the element
*/
@Override
public int indexOf(Object o) {
for (int i = 0; i < size; i++) {
if (elementData[i].equals(o)) {
return i;
}
}
return -1;
}
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
@Override
public boolean isEmpty() {
return size == 0;
}
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left.
* Returns the element that was removed from the list.
*
* @param index - the index of the element to be removed
* @return the element previously at the specified position
*/
@Override
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
E oldElement = (E) elementData[index];
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
elementData[size] = null;
size--;
// TODO: probably should write trim function
return oldElement;
}
/**
* Removes the first occurrence of the specified element from this list, if it
* is present.
* If this list does not contain the element, it is unchanged.
* Returns true if this list contained the specified element.
*
* @param o - element to be removed from this list, if present
* @return true if this list contained the specified element
*/
@Override
public boolean remove(Object o) {
int index = indexOf(o);
if (index >= 0) {
remove(index);
return true;
}
return false;
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index - index of the element to replace
* @param element - element to be stored at the specified position
* @return the element previously at the specified position
*/
@Override
public E set(int index, E element) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
E oldElement = (E) elementData[index];
elementData[index] = element;
return oldElement;
}
/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/
@Override
public int size() {
return size;
}
/**
* Sorts this list.
*/
@Override
public void sort() {
Sort.quicksort(elementData, 0, size - 1);
}
/**
* Sorts this list using comparator.
*
* @param c - comparator
*/
@Override
public void sort(Comparator<? super E> c) {
Sort.quicksort(elementData, 0, size - 1, c);
}
private boolean ensureCapacity(int currentSize) {
return currentSize + 1 == elementData.length;
}
private void increaseCapacity(int oldCapacity) {
Object[] newElementData = new Object[oldCapacity * 3 / 2 + 1];
System.arraycopy(elementData, 0, newElementData, 0, size);
elementData = newElementData;
}
}