-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
52 lines (44 loc) · 1.09 KB
/
ArrayList.java
File metadata and controls
52 lines (44 loc) · 1.09 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
public class ArrayList<T> implements List<T> {
T[] arr;
int size;
public ArrayList() {
T[] temp = (T[]) new Object[10];
arr = temp;
size = 0;
}
public void add(T item) {
add(size, item);
}
public void add(int pos, T item) {
Assert.not_false(pos > 0 || pos < size + 1);
if(pos == arr.length) { growArray(); }
for(int i = size; pos < i; i++) {
arr[i] = arr[i - 1];
}
arr[pos] = item;
size++;
}
protected void growArray() {
T[] temp = (T[]) new Object[size * 2];
for(int i = 0; i < size; i++) {
temp[i] = arr[i];
}
arr = temp;
}
public T get(int pos) {
Assert.not_false(pos >= 0 && pos < size);
return arr[pos];
}
public T remove(int pos) {
Assert.not_false(pos >= 0 && pos < size);
T removed = arr[pos];
for(int i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
return removed;
}
public int size() {
return size;
}
}