-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
55 lines (52 loc) · 1.19 KB
/
ArrayList.java
File metadata and controls
55 lines (52 loc) · 1.19 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
public class ArrayList<T> implements List<T> {
private int maxsize;
private int size;
private int current;
private T [] nodes;
/** Creates a new instance of ArrayList */
public ArrayList(int n) {
maxsize = n;
size = 0;
current = -1;
nodes = (T[]) new Object[n];
}
public boolean full() {
return size == maxsize;
}
public boolean empty() {
return size == 0;
}
public boolean last() {
return current == size - 1;
}
public void findFirst() {
current = 0;
}
public void findNext() {
current++;
}
public T retrieve() {
return nodes[current];
}
public void update(T val) {
nodes[current] = val;
}
public void insert(T val) {
for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
public void remove() {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
if (size == 0)
current = -1;
else if (current == size)
current = 0;
}
}