Skip to content

Commit 97b5bcb

Browse files
committed
add js ArrayList
1 parent bae1300 commit 97b5bcb

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

javascript/ArrayList/ArrayList.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// 迭代器
2+
function Iterator(list) {
3+
this.list = list;
4+
this.current = 0;
5+
}
6+
7+
Iterator.prototype.hasNext = function() {
8+
return this.current < this.list.size();
9+
};
10+
11+
Iterator.prototype.next = function() {
12+
if (!this.hasNext()) {
13+
throw new Error('No Such Element Exception!');
14+
}
15+
16+
return this.list.get(current++);
17+
};
18+
19+
Iterator.prototype.remove = function() {
20+
this.list.remove(--current);
21+
};
22+
23+
24+
// ArrayList 构造函数
25+
function ArrayList() {
26+
this.dataStore = [];
27+
}
28+
29+
ArrayList.prototype.size = function() {
30+
return this.dataStore.length;
31+
};
32+
33+
ArrayList.prototype.get = function(index) {
34+
if (index < 0 || index >= this.size()) {
35+
throw new Error('index:' + index + ' out of bounds exception!');
36+
}
37+
38+
return this.dataStore[index];
39+
};
40+
41+
ArrayList.prototype.set = function(index, newVal) {
42+
if (index < 0 || index >= this.size()) {
43+
throw new Error('index:' + index + ' out of bounds exception!');
44+
}
45+
46+
this.dataStore[index] = newVal;
47+
};
48+
49+
ArrayList.prototype.insert = function(index, val) {
50+
if (index < 0 || index >= this.size()) {
51+
throw new Error('index:' + index + ' out of bounds exception!');
52+
}
53+
54+
this.dataStore.splice(index, 0, val);
55+
};
56+
57+
ArrayList.prototype.append = function(val) {
58+
this.dataStore.push(val);
59+
};
60+
61+
ArrayList.prototype.remove = function(index) {
62+
if (index < 0 || index >= this.size()) {
63+
throw new Error('index:' + index + ' out of bounds exception!');
64+
}
65+
66+
this.dataStore.splice(index, 1);
67+
};
68+
69+
ArrayList.prototype.clear = function() {
70+
this.dataStore = [];
71+
};
72+
73+
ArrayList.prototype.find = function(val) {
74+
for (var i = 0; i < this.dataStore.length; i++) {
75+
if (this.dataStore[i] === val) return i;
76+
}
77+
return -1;
78+
};
79+
80+
ArrayList.prototype.iterator = function() {
81+
return new Iterator(this);
82+
};
83+
84+
ArrayList.prototype.toString = function() {
85+
return this.dataStore.join();
86+
};
87+
88+
module.exports = ArrayList;

javascript/test/ArrayList_test.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var assert = require('assert');
2+
var ArrayList = require('../ArrayList/ArrayList.js');
3+
4+
describe('ArrayList', function() {
5+
describe('#size()', function() {
6+
it('should return 0 when the ArrayList is empty', function() {
7+
var arrayList = new ArrayList();
8+
assert.equal(0, arrayList.size());
9+
});
10+
11+
it('should return valid value', function() {
12+
var arrayList = new ArrayList();
13+
arrayList.append(1);
14+
arrayList.append(2);
15+
arrayList.append(3);
16+
assert.equal(3, arrayList.size());
17+
18+
arrayList.remove(2);
19+
assert.equal(2, arrayList.size());
20+
});
21+
});
22+
23+
describe('#get(index)', function() {
24+
it('should return valid value', function() {
25+
var arrayList = new ArrayList();
26+
arrayList.append(1);
27+
arrayList.append(2);
28+
arrayList.append(3);
29+
assert.equal(3, arrayList.get(2));
30+
assert.equal(2, arrayList.get(1));
31+
});
32+
});
33+
34+
describe('#set(index, newVal)', function() {
35+
it('should set success and have no error', function() {
36+
var arrayList = new ArrayList();
37+
arrayList.append(1);
38+
arrayList.append(2);
39+
arrayList.append(3);
40+
assert.equal(3, arrayList.get(2));
41+
42+
assert.doesNotThrow(function() {
43+
arrayList.set(2, 4);
44+
});
45+
assert.equal(4, arrayList.get(2));
46+
47+
assert.throws(function() {
48+
arrayList.set(3, 4);
49+
}, Error);
50+
});
51+
});
52+
53+
describe('#insert(index, val)', function() {
54+
it('should insert success and have no error', function() {
55+
var arrayList = new ArrayList();
56+
arrayList.append(1);
57+
assert.doesNotThrow(function() {
58+
arrayList.insert(0, 2);
59+
});
60+
61+
assert.equal(2, arrayList.get(0));
62+
assert.equal(2, arrayList.size());
63+
});
64+
});
65+
66+
describe('#append(val)', function() {
67+
it('should append success and have no error', function() {
68+
var arrayList = new ArrayList();
69+
arrayList.append(1);
70+
71+
assert.equal(1, arrayList.get(0));
72+
assert.equal(1, arrayList.size());
73+
});
74+
});
75+
76+
describe('#remove(index)', function() {
77+
it('should remove success and have no error', function() {
78+
var arrayList = new ArrayList();
79+
arrayList.append(1);
80+
arrayList.append(2);
81+
arrayList.append(3);
82+
assert.equal(3, arrayList.size());
83+
84+
arrayList.remove(0);
85+
assert.equal(2, arrayList.get(0));
86+
assert.equal(2, arrayList.size());
87+
});
88+
});
89+
90+
describe('#clear()', function() {
91+
it('should clear success and have no error', function() {
92+
var arrayList = new ArrayList();
93+
arrayList.append(1);
94+
arrayList.append(2);
95+
arrayList.append(3);
96+
97+
arrayList.clear();
98+
assert.equal(0, arrayList.size());
99+
});
100+
});
101+
102+
describe('#find(val)', function() {
103+
it('should return a valid value', function() {
104+
var arrayList = new ArrayList();
105+
arrayList.append(1);
106+
arrayList.append(2);
107+
arrayList.append(3);
108+
109+
arrayList.find(2);
110+
assert.equal(1, arrayList.find(2));
111+
});
112+
});
113+
114+
describe('#iterator()', function() {
115+
it('should return a valid value', function() {
116+
var arrayList = new ArrayList();
117+
arrayList.append(1);
118+
arrayList.append(2);
119+
var iterator = arrayList.iterator();
120+
assert.notEqual(undefined, iterator);
121+
assert.notEqual(undefined, iterator.next);
122+
});
123+
});
124+
125+
describe('#toString()', function() {
126+
it('should return a valid value', function() {
127+
var arrayList = new ArrayList();
128+
arrayList.append(1);
129+
arrayList.append(2);
130+
131+
assert.equal('1,2', arrayList.toString());
132+
});
133+
});
134+
135+
});

0 commit comments

Comments
 (0)