-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicArray.java
More file actions
90 lines (72 loc) · 1.94 KB
/
DynamicArray.java
File metadata and controls
90 lines (72 loc) · 1.94 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
81
82
83
84
85
86
87
88
89
90
public class DynamicArray<Type>
{
//All the data
Object data;
private void out(String message)
{
kak.out("DynamicArray", message)
}
public DynamicArray(int size)
{
//Create a new array of the given size `size`
data = new Object[size];
//Debugging
out("Dynamic array of size \"" + size + "\" created.");
}
private void expandAndAppend(Type element)
{
//debugging
out("Expansion occurring from size \"" + data.length + "\" top size \"" + (data.length+1) + "\"...");
//New array of size n+1 (where n is `data`'s size')
Object[] newData = new Object[data.length+1];
//Copy all the data from the array referenced by `data` to the
//array referenced by `newData`
for(int i = 0; i < data.length; i++)
{
newData[i] = data[i];
}
//Append the new element
newData[data.length] = element;
//Update the reference held by `data` to now point to the array refrenced by `newData`
data = newData;
//Debugging
out("Expansion finshed.");
}
//Append a new element to the array
public void append(Type element)
{
expandAndAppend(element);
}
//Get an element at index `index`
public Type get(int index)
{
return (Type)data[index];
}
//Get an array of all the data
public Type[] getArray()
{
return (Type[])data;
}
//Remove an element at the given index `index`
public void remove(int index)
{
Object[] newData = new Object[data.length-1];
//Index count
int indexPointer = 0;
//Iterate over the array and add all elemnets from `data` to `newData` besides the one at index `index`
for(int i = 0; i < data.length; i++)
{
if(i == index)
{
//Do nothing
}
else
{
newData[indexPointer] = data[i];
indexPointer++;
}
}
//Update the reference held by `data` to point to a new object pointed to the object that is referenced by `newData`
data = newData;
}
}