Repository navigation
Expand file tree
/
Copy pathArrayListExample.java
More file actions
104 lines (81 loc) · 2.51 KB
/
Copy pathArrayListExample.java
File metadata and controls
104 lines (81 loc) · 2.51 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
package DSA.Arrays;
import java.util.Arrays;
public class ArrayListExample<E> {
private static final int DEFAULT_CAPACITY = 10;
private Object[] elementData;
private int size =0;
public ArrayListExample() {
this.elementData = new Object[DEFAULT_CAPACITY];
}
public void add(E e) {
ensureCapacity();
this.elementData[size++] = e;
}
public E get(int index) {
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
}
return (E) this.elementData[index];
}
public E remove(int index) {
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
}
E oldValue = (E) this.elementData[index];
int lengthToMove = this.size - 1 - index;
if (lengthToMove > 0) {
System.arraycopy(this.elementData, index + 1, this.elementData, index, lengthToMove);
}
this.elementData[--this.size] = null ;// reduce size
return oldValue;
}
private void ensureCapacity() {
int length = this.elementData.length;
if (this.size == length) {
int newCapacity = length + (length >> 1);// right shift 1 bit = 1.5 times
this.elementData = Arrays.copyOf(this.elementData, newCapacity);
}
}
private void printList(ArrayListExample<Integer> l) {
for (int i = 0; i < l.size() ; i++) {
System.out.println(l.get(i));
}
}
private int size() {
return this.size;
}
public Iterator<Object> createIterator() {
return new ArrayListIterator(elementData); // Return a PlaylistIterator
}
public static void main(String[] args) {
ArrayListExample<Integer> ll = new ArrayListExample<>();
ll.add(1);
ll.add(2);
ll.add(3);
ll.printList(ll);
ll.remove(2);
ll.printList(ll);
}
public interface Iterator<T> {
boolean hasNext();
T next();
}
}
class ArrayListIterator implements ArrayListExample.Iterator<Object> {
Object[] elements;
int position = 0;
ArrayListIterator(Object[] elements) {
this.elements = elements;
}
@Override
public boolean hasNext() {
return position < elements.length;
}
@Override
public Object next() {
if (hasNext()) {
return elements[position++];
}
return null;
}
}