Skip to content

Commit fc5d757

Browse files
committed
Merge pull request processing-js#22 from davidleibovic/master
[#1885] Fixed ArrayList Iterator
2 parents d53136f + 7fd1a8e commit fc5d757

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

processing.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,17 @@
454454
*/
455455
var ArrayList = (function() {
456456
function Iterator(array) {
457-
var index = 0;
457+
var index = -1;
458458
this.hasNext = function() {
459-
return index < array.length;
459+
return (index + 1) < array.length;
460460
};
461461

462462
this.next = function() {
463-
return array[index++];
463+
return array[++index];
464464
};
465465

466466
this.remove = function() {
467-
array.splice(index, 1);
467+
array.splice(index--, 1);
468468
};
469469
}
470470

test/unit/arrayListIterator.pde

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
ArrayList<String> arrayList = new ArrayList<String>();
2+
arrayList.add("a");
3+
arrayList.add("b");
4+
arrayList.add("c");
5+
6+
_checkEqual(3, arrayList.size());
7+
8+
Iterator<String> it = arrayList.iterator();
9+
String temp;
10+
11+
12+
// test iteration methods
13+
_checkTrue(it.hasNext());
14+
temp = it.next();
15+
_checkEqual(temp, "a");
16+
17+
_checkTrue(it.hasNext());
18+
temp = it.next();
19+
_checkEqual(temp, "b");
20+
21+
_checkTrue(it.hasNext());
22+
temp = it.next();
23+
_checkEqual(temp, "c");
24+
25+
_checkFalse(it.hasNext());
26+
27+
// test remove method
28+
it = arrayList.iterator();
29+
30+
ArrayList<String> remove1 = new ArrayList<String>();
31+
remove1.add("b");
32+
remove1.add("c");
33+
34+
ArrayList<String> remove2 = new ArrayList<String>();
35+
remove2.add("c");
36+
37+
ArrayList<String> remove3 = new ArrayList<String>();
38+
39+
40+
_checkTrue(it.hasNext());
41+
it.next();
42+
it.remove();
43+
_checkEqual(2, arrayList.size());
44+
_checkEqual(remove1, arrayList);
45+
46+
_checkTrue(it.hasNext());
47+
it.next();
48+
it.remove();
49+
_checkEqual(1, arrayList.size());
50+
_checkEqual(remove2, arrayList);
51+
52+
_checkTrue(it.hasNext());
53+
it.next();
54+
it.remove();
55+
_checkEqual(0, arrayList.size());
56+
_checkEqual(remove3, arrayList);
57+
58+
_checkFalse(it.hasNext());

0 commit comments

Comments
 (0)