File tree Expand file tree Collapse file tree 2 files changed +62
-4
lines changed Expand file tree Collapse file tree 2 files changed +62
-4
lines changed Original file line number Diff line number Diff line change 454
454
*/
455
455
var ArrayList = (function() {
456
456
function Iterator(array) {
457
- var index = 0 ;
457
+ var index = -1 ;
458
458
this.hasNext = function() {
459
- return index < array.length;
459
+ return ( index + 1) < array.length;
460
460
};
461
461
462
462
this.next = function() {
463
- return array[index++ ];
463
+ return array[++index ];
464
464
};
465
465
466
466
this.remove = function() {
467
- array.splice(index, 1);
467
+ array.splice(index-- , 1);
468
468
};
469
469
}
470
470
Original file line number Diff line number Diff line change
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());
You can’t perform that action at this time.
0 commit comments