-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicArray.java
More file actions
142 lines (113 loc) · 3.29 KB
/
DynamicArray.java
File metadata and controls
142 lines (113 loc) · 3.29 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
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
import java.util.Iterator;
@SuppressWarnings("unchecked")
public class DynamicArray<T> implements Iterable<T> {
public static final int DEFAULT_CAPACITY = 16;
private T[] array;
private int len = 0;
private int capacity;
public DynamicArray() {
array = (T[]) new Object [DEFAULT_CAPACITY];
capacity = DEFAULT_CAPACITY;
}
public DynamicArray(int capacity) {
array = (T[]) new Object [capacity];
this.capacity = capacity;
}
public T[] getArray() {
return array;
}
public int getLen() {
return len;
}
public T get(int index) {
if (index < 0 || index >= len)
return null;
return array[index];
}
public void set(int index, T item) {
if (index < 0 || index >= len)
return;
array[index] = item;
}
void optionallyResize(int targetCapacity, int newCapacity) {
if (len == targetCapacity) {
T[] newArray = (T[]) new Object [newCapacity];
for (int i = 0; i < targetCapacity; i++)
newArray[i] = array[i];
capacity = newCapacity;
array = newArray;
}
}
public void add(T item) {
optionallyResize(capacity, 2 * capacity);
array[len++] = item;
}
public Boolean remove(int index) {
if (index < 0 || index >= len)
return false;
// O(n)
int len_prev = len - 1;
for (int i = index; i < len_prev; i++)
array[i] = array[i + 1];
len--;
int halfCapacity = capacity / 2;
optionallyResize(halfCapacity, halfCapacity);
return true;
}
@Override
public DynamicArrayIterator iterator() {
return new DynamicArrayIterator<T>(this);
}
@Override
public String toString() {
if (len == 0)
return "[]";
StringBuilder sb = new StringBuilder();
sb.append("[" + array[0]);
for (int i = 1; i < len; i++)
sb.append(", " + array[i]);
sb.append("]");
return sb.toString();
}
public static void main(String[] args) {
DynamicArray<Integer> da = new DynamicArray<>();
da.add(10);
da.add(20);
da.add(30);
System.out.println(da.get(0));
System.out.println(da.get(1));
System.out.println(da.get(2));
da.remove(0);
System.out.println(da);
da.remove(1);
System.out.println(da);
da.remove(0);
System.out.println(da);
for (int i = 0; i < 10000; i++)
da.add(i);
for (Integer item : da)
System.out.println(item);
//System.out.println(da);
for (int i = 0; i < 10000; i++)
da.remove(0);
System.out.println(da);
DynamicArray<Integer> da1 = new DynamicArray<>(64);
}
}
class DynamicArrayIterator<T> implements Iterator<T> {
T[] array;
int len;
int current = 0;
public DynamicArrayIterator(DynamicArray<T> da) {
array = da.getArray();
len = da.getLen();
}
// returns false if next element does not exist
public boolean hasNext() {
return current < len;
}
// returns current data and update current;
public T next() {
return array[current++];
}
}