-
Notifications
You must be signed in to change notification settings - Fork 6
/
PartialIntArray.java
executable file
·52 lines (42 loc) · 1.16 KB
/
PartialIntArray.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
42
43
44
45
46
47
48
49
50
51
52
public class PartialIntArray {
private int[] elements;
private int size;
public PartialIntArray() {
this(10);
}
public PartialIntArray(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal Capacity: "
+ initialCapacity);
}
this.elements = new int[initialCapacity];
}
public void add(int e) {
if (size >= elements.length) {
int[] resizedArray = new int[elements.length * 2];
for (int i = 0; i < elements.length; i++) {
//TODO add code here!
}
}
elements[size++] = e;
}
public int get(int index) {
if (index < 0 || index >= size) {
//TODO add code here!
}
return elements[index];
}
public int size() {
return this.size;
}
public static void main(String[] args) {
PartialIntArray pia = new PartialIntArray();
pia.add(2);
pia.add(4);
pia.add(6);
for (int i = 0; i < pia.size(); i++) {
System.out.println(pia.get(i));
}
pia.get(10);
}
}