-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynamicArray.java
41 lines (34 loc) · 1.07 KB
/
DynamicArray.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 amortizedAnalysis.dynamicarray;
import java.util.Arrays;
public class DynamicArray {
private String[] data;
private int max = 0;
public String[] getData(){
return data;
}
public DynamicArray(int initialSize){
this.data = new String[initialSize];
}
public void add(String data){
if(this.data.length == this.max){
String[] newData = new String[this.max * 2];
for(int i = 0; i < this.max; i++){
newData[i] = this.data[i];
}
this.data = newData;
}
this.data[max++] = data;
}
public static void main(String[] args){
DynamicArray da = new DynamicArray(1);
da.add("a");
System.out.println(da.data.length);
System.out.println(Arrays.toString(da.getData()));
da.add("b");
System.out.println(da.data.length);
System.out.println(Arrays.toString(da.getData()));
da.add("c");
System.out.println(da.data.length);
System.out.println(Arrays.toString(da.getData()));
}
}