-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDArray 2.java
More file actions
49 lines (44 loc) · 915 Bytes
/
DArray 2.java
File metadata and controls
49 lines (44 loc) · 915 Bytes
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
class DynamicArray{
private int arr[];
private int size;
private int count;
public DynamicArray()
{
this.size = 1;
this.arr = new int [size];
this.count = 0;
}
public void insert(int value)
{
CheckAndGrow();
arr[count]=value;
count++;
}
public void display()
{
for(int i:arr)
{
System.out.print(i+" ");
}
}
private void CheckAndGrow()
{
if(this.size == this.count)
{
int temparr[] = new int[size*2];
for(int i=0;i<count;i++)
{
temparr[i]=arr[i];
}
arr = temparr;
size = size*2;
}
}
}
public class DArray {
public static void main(String[] args) {
DynamicArray d = new DynamicArray();
d.insert(5);
d.insert(6);
}
}