forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChap03.question.16.MyArrayListReverseIterator.java
239 lines (200 loc) · 6.09 KB
/
Chap03.question.16.MyArrayListReverseIterator.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//Chap03.question.16.MyArrayListReverseIterator.java
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class MyArrayList<T> implements Iterable<T> {
private static final int DEFAULT_CAPACITY = 10;
private Object[] elements;
private int size;
private int capacity;
private int modCount;
public MyArrayList() {
this(DEFAULT_CAPACITY);
}
public MyArrayList(int newCapacity) {
if (newCapacity < DEFAULT_CAPACITY) {
capacity = DEFAULT_CAPACITY;
} else {
capacity = newCapacity;
}
size = 0;
elements = new Object[this.capacity];
modCount = 0;
}
public void add(T t) {
if (size == capacity) {
ensureCapacity(capacity + capacity >>> 1);
}
elements[size++] = t;
modCount++;
}
public void addAt(int index, T t) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
if (size == capacity)
ensureCapacity(capacity + capacity >>> 1);
System.arraycopy(elements, index, elements, index + 1, size - index - 1);
elements[index] = t;
modCount++;
size++;
}
private void ensureCapacity(int newCapacity) {
if (newCapacity <= capacity) return;
Object[] newArr = new Object[newCapacity];
System.arraycopy(elements, 0, newArr, 0, size);
elements = newArr;
}
public boolean remove(T t) {
if (t == null) {
for (int i = 0; i < size; i++) {
if (elements[i] == null) {
removeAt(i);
return true;
}
}
} else {
for (int i = 0; i < size; i++) {
if (t.equals(elements[i])) {
removeAt(i);
return true;
}
}
}
return false;
}
public void removeAt(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
System.arraycopy(elements, index + 1, elements, index, size - index - 1);
size--;
modCount++;
}
@SuppressWarnings("unchecked")
public T get(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
return (T) elements[index];
}
public void set(int index, T t) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
elements[index] = t;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(T t) {
if (t == null) {
for (int i = 0; i < size; i++)
if (elements[i] == null)
return true;
} else {
for (int i = 0; i < size; i++)
if (t.equals(elements[i]))
return true;
}
return false;
}
@Override
public Iterator<T> iterator() {
return new MyArrayListIterator();
}
public Iterator<T> reversedIterator() {
return new ReversedIterator();
}
private class MyArrayListIterator implements ListIterator<T> {
private int cursor = 0;
private boolean canModify = false;
@Override
public boolean hasNext() {
return cursor < size;
}
@SuppressWarnings("unchecked")
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
checkForComodification();
Object nextItem = elements[cursor++];
canModify = true;
return (T) nextItem;
}
private void checkForComodification() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
}
@Override
public boolean hasPrevious() {
return cursor > 0;
}
@SuppressWarnings("unchecked")
@Override
public T previous() {
if (!hasPrevious())
throw new NoSuchElementException();
checkForComodification();
canModify = true;
return (T) elements[cursor--];
}
@Override
public int nextIndex() {
return cursor;
}
@Override
public int previousIndex() {
return cursor - 1;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(T t) {
checkForComodification();
elements[cursor] = t;
}
@Override
public void add(T t) {
if (!canModify)
throw new IllegalStateException();
checkForComodification();
MyArrayList.this.addAt(cursor++, t);
expectedModCount++;
canModify = false;
}
private int expectedModCount = modCount;
}
private class ReversedIterator implements Iterator<T> {
private boolean canRemove = false;
@Override
public boolean hasNext() {
return cursor >= 0;
}
@SuppressWarnings("unchecked")
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
canRemove = true;
return (T) elements[cursor--];
}
@Override
public void remove() {
if (!canRemove)
throw new IllegalStateException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
MyArrayList.this.removeAt(cursor + 1);
expectedModCount++;
canRemove = false;
}
private int cursor = size - 1;
private int expectedModCount = modCount;
}
}