forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap03.question.13.MyArrayListWithListIterator.java
197 lines (168 loc) · 5.12 KB
/
Chap03.question.13.MyArrayListWithListIterator.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
//Chap03.question.13.MyArrayListWithListIterator.java
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class MyArrayList<E> implements Iterable<E> {
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 capacity) {
if (capacity < DEFAULT_CAPACITY) {
this.capacity = DEFAULT_CAPACITY;
} else {
this.capacity = capacity;
}
size = 0;
elements = new Object[this.capacity];
modCount = 0;
}
public void add(E e) {
if (size == capacity) {
ensureCapacity(capacity + capacity >> 1);
}
elements[size++] = e;
modCount++;
}
public void addAt(int index, E e) {
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] = e;
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(E e) {
if (e == 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 (e.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 E get(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
return (E) elements[index];
}
public void set(int index, E e) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
elements[index] = e;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(E e) {
if (e == null) {
for (int i = 0; i < size; i++)
if (elements[i] == null)
return true;
} else {
for (int i = 0; i < size; i++)
if (e.equals(elements[i]))
return true;
}
return false;
}
@Override
public Iterator<E> iterator() {
return new MyArrayListIterator();
}
private class MyArrayListIterator implements ListIterator<E> {
private int cursor = 0;
private boolean canModify = false;
@Override
public boolean hasNext() {
return cursor < size;
}
private int expectedModCount = modCount;
@SuppressWarnings("unchecked")
@Override
public E next() {
if (!hasNext())
throw new NoSuchElementException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
Object nextItem = elements[cursor++];
canModify = true;
return (E) nextItem;
}
@Override
public boolean hasPrevious() {
return cursor > 0;
}
@SuppressWarnings("unchecked")
@Override
public E previous() {
if (!hasPrevious())
throw new NoSuchElementException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
canModify = true;
return (E) 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(E e) {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
elements[cursor] = e;
}
@Override
public void add(E e) {
if (!canModify)
throw new IllegalStateException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
MyArrayList.this.addAt(cursor++, e);
expectedModCount++;
canModify = false;
}
}
}