forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChap03.question.09.MyArrayListAddAll.java
More file actions
112 lines (96 loc) · 2.96 KB
/
Copy pathChap03.question.09.MyArrayListAddAll.java
File metadata and controls
112 lines (96 loc) · 2.96 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
//Chap03.question.09.MyArrayListAddAll.java
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyArrayList<E> implements Iterable<E> {
private Object[] elements;
private int size;
private int capacity;
private static final int DEFAULT_CAPACITY = 10;
private int modCount;
public MyArrayList() {
size = 0;
modCount = 0;
capacity = DEFAULT_CAPACITY;
elements = new Object[capacity];
}
public void add(E e) {
if (size == capacity) {
ensureCapacity(capacity + capacity >>> 1);
}
elements[size++] = e;
modCount++;
}
public void addAll(Collection<E> items) {
ensureCapacity(capacity + items.size());
for (E e : items)
add(e);
}
@SuppressWarnings("unchecked")
public void addAll(Iterable<? extends E> items) {
Iterator iterator = items.iterator();
while (iterator().hasNext()){
add((E)iterator.next());
}
}
private void ensureCapacity(int i) {
if (i <= capacity) return;
Object[] temp = new Object[i];
System.arraycopy(elements, 0, temp, 0, size);
elements = temp;
capacity = i;
}
@SuppressWarnings("unchecked")
public E get(int index) {
if (index < 0 || index >= size)
throw new ArrayIndexOutOfBoundsException();
return (E) elements[index];
}
public void removeAt(int index) {
if (index < 0 || index >= size)
throw new ArrayIndexOutOfBoundsException();
System.arraycopy(elements, index + 1, elements, index, size - index - 1);
size--;
modCount++;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
@Override
public Iterator<E> iterator() {
return new MyArrayListIterator();
}
private class MyArrayListIterator implements Iterator<E> {
private int currentIndex = 0;
private int expectedModCount = modCount;
private boolean canRemove = false;
@Override
public boolean hasNext() {
return currentIndex < size;
}
@SuppressWarnings("unchecked")
@Override
public E next() {
if (!hasNext())
throw new NoSuchElementException();
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
canRemove = true;
return (E) elements[currentIndex++];
}
@Override
public void remove() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
if (!canRemove)
throw new IllegalStateException();
removeAt(currentIndex - 1);
expectedModCount++;
canRemove = false;
}
}
}