-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
LongList.java
41 lines (34 loc) · 884 Bytes
/
LongList.java
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
package nallar.collections;
import java.util.*;
public class LongList {
private static final long[] EMPTY_LIST = new long[0];
public int size = 0;
public long[] list;
public LongList() {
list = EMPTY_LIST;
}
public LongList(int initialSize) {
list = new long[initialSize];
}
public void add(long value) {
int index = size++;
long[] list = this.list;
int length = list.length;
if (index >= length) {
this.list = list = Arrays.copyOf(list, length == 0 ? 10 : length * 2);
}
list[index] = value;
}
public void set(int index, long value) {
if (index >= size) {
throw new ArrayIndexOutOfBoundsException(index + " > " + size);
}
list[index] = value;
}
public long get(int index) {
if (index >= size) {
throw new ArrayIndexOutOfBoundsException(index + " > " + size);
}
return list[index];
}
}