-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList.java
221 lines (191 loc) · 5.32 KB
/
ArrayList.java
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
import java.util.NoSuchElementException;
/**
* Class implementing Array based List.
*
* @param <E> type of the stored elements.
*/
class ArrayList<E> implements List<E> {
// current size
private int size;
private int capacity;
private Object[] data;
/**
* Constructs empty array-based list.
*/
ArrayList() {
data = new Object[20];
capacity = 20;
size = 0;
}
/**
* Return size of the list.
*
* @return size
*/
public int size() {
return this.size;
}
/**
* Check whether the list is empty.
*
* @return empty or not
*/
public boolean isEmpty() {
return this.size == 0;
}
/**
* Get i-th element.
*
* @param i index of the element
* from 0 to size-1
* @return element at the index
* @throws IndexOutOfBoundsException
* if index is not in the list
*/
@SuppressWarnings("unchecked")
public E get(int i) throws IndexOutOfBoundsException {
if (i < 0 || i >= this.size) {
throw new IndexOutOfBoundsException();
}
return (E) data[i];
}
/**
* Set i-th element.
*
* @param i index of the element
* from 0 to size-1
* @param e new value for the element
* @return the replaced (old) element
* @throws IndexOutOfBoundsException
* if index is not in the list
*/
@SuppressWarnings("unchecked")
public E set(int i, E e) throws IndexOutOfBoundsException {
if (i < 0 || i >= this.size) {
throw new IndexOutOfBoundsException();
}
data[i] = e;
return (E) data[i];
}
/**
* Add new element at index i.
*
* @param i index of the element
* from 0 to size
* @param e element to be added
* @throws IndexOutOfBoundsException
* if index is not in the list
*/
public void add(int i, E e) throws IndexOutOfBoundsException {
if (i < 0 || i > this.size) {
throw new IndexOutOfBoundsException();
}
if (this.size + 1 > this.capacity) {
Object[] new_data = new Object[this.capacity * 2];
for (int j = 0; j < i; j++) {
new_data[j] = this.data[j];
}
new_data[i] = e;
for (int j = i + 1; j < this.data.length; j++) {
new_data[j] = this.data[j - 1];
}
this.capacity *= 2;
this.size++;
this.data = new_data;
return;
}
for (int j = i + 1; j < this.data.length; j++) {
this.data[j] = this.data[j - 1];
}
this.data[i] = e;
this.size++;
}
/**
* Remove the element at index i.
*
* @param i index of the element
* from 0 to size-1
* @return the removed element
* @throws IndexOutOfBoundsException
* if index is not in the list
*/
@SuppressWarnings("unchecked")
public E remove(int i) throws IndexOutOfBoundsException {
if (i < 0 || i >= this.size) {
throw new IndexOutOfBoundsException();
}
E removed = (E) data[i];
for (int j = i; j < this.data.length; j++) {
this.data[j] = this.data[j + 1];
}
this.data[this.size - 1] = null;
this.size--;
return removed;
}
/**
* Change the size of the ArrayList removing elements with
* indexes less than newSize.
*
* @param newSize new size of the ArrayList
*/
public void resize(int newSize) {
if (this.size < newSize) {
Object[] newData = new Object[newSize];
for (int i = 0; i < this.size; i++) {
newData[i] = this.data[i];
}
this.capacity = newSize;
this.size = newSize;
this.data = newData;
return;
}
for (int i = newSize - 1; i < this.size; i++) {
this.data[i] = null;
}
this.size = newSize;
}
/**
* Returns an iterator over the elements of the ArrayList.
*
* @return an iterator
*/
public Iterator<E> iterator() {
return new ArrayListIterator(this);
}
/**
* Iterator over the ArrayList.
*/
private class ArrayListIterator implements Iterator<E> {
ArrayList<E> data;
int nextIndex;
/**
* Constructs new iterator over the elements of ArrayList.
*
* @param current corresponding ArrayList o
*/
private ArrayListIterator(ArrayList<E> current) {
nextIndex = 0;
data = current;
}
/**
* Checks whether there are next elements in the ArrayList.
*
* @return true if there is element in the ArrayList, false otherwise
*/
public boolean hasNext() {
return nextIndex < data.size;
}
/**
* Returns the next element in the ArrayList.
*
* @return the next element if it exists
* @throws NoSuchElementException if the element doesn't exist
*/
public E next() throws NoSuchElementException {
if (!hasNext()) {
throw new NoSuchElementException();
}
return data.get(nextIndex++);
}
}
}