Repository navigation
Expand file tree
/
Copy pathDynamicArray.java
More file actions
80 lines (65 loc) · 1.62 KB
/
Copy pathDynamicArray.java
File metadata and controls
80 lines (65 loc) · 1.62 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
package adt;
public class DynamicArray<E> {
private static final int DEFAULT_SIZE = 10;
private int size;
private Object[] arr;
public DynamicArray() {
this.size = 0;
this.arr = new Object[DEFAULT_SIZE];
}
public DynamicArray(int size) {
if(size < 0) throw new IllegalArgumentException();
this.size = size;
this.arr = new Object[this.size];
}
public E get(int index) {
if(index >= this.size) throw new IllegalArgumentException();
return (E)this.arr[index];
}
public int size() {
return this.size;
}
public void set(int index, E value) {
if(index >= this.arr.length) {
int newSize = this.newSize(index);
this.sizeUp(newSize);
}
if(index+1 > this.size) this.size = index+1;
this.arr[index] = value;
}
public E remove(int index) {
if(index >= this.size) throw new IllegalArgumentException();
E value = (E)this.arr[index];
this.shiftLeft(index);
return value;
}
private void shiftLeft(int index) {
this.size--;
for(int i = index; i < this.size; i++) {
this.arr[i] = this.arr[i+1];
}
}
private int newSize(int index) {
int curSize = this.arr.length;
while(index >= curSize) curSize *= 2;
return curSize;
}
private void sizeUp(int newSize) {
Object[] temp = new Object[newSize];
for(int i = 0; i < this.size; i++) {
temp[i] = this.arr[i];
}
this.arr = temp;
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
sb.append("[");
for(int i = 0; i < this.size - 1; i++) {
sb.append(this.arr[i] + ", ");
}
sb.append(this.arr[this.size-1] + "]");
return sb.toString();
}
}