-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayList.js
51 lines (45 loc) · 1.47 KB
/
ArrayList.js
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
// ****************************************************************************
class ArrayList {
// ------------------------------------------------------------------------
constructor() {
this.length = 0;
this.data = {};
}
// ------------------------------------------------------------------------
get(index) {
return this.data[index];
}
// ------------------------------------------------------------------------
push(item) {
this.data[this.length] = item;
this.length++;
return this.length;
}
// ------------------------------------------------------------------------
pop() {
const lastItem = this.data[this.length-1];
delete this.data[this.length-1];
this.length--;
}
// ------------------------------------------------------------------------
delete(index) {
const item = this.data[index];
this.shiftItems(index);
}
// ------------------------------------------------------------------------
shiftItems(index) {
for (let i = index; i < this.length - 1; i++) {
this.data[i] = this.data[i + 1];
}
delete this.data[this.length-1];
this.length--;
}
}
// ****************************************************************************
const newArray = new ArrayList();
newArray.push('hello');
newArray.push('you');
newArray.push('!');
// newArray.pop();
newArray.delete(1);
console.log(newArray);