-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_list.dart
61 lines (49 loc) · 1.15 KB
/
array_list.dart
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
import 'dart:collection';
class ArrayList<T> extends Object with IterableMixin<T> {
int _size = 0;
List<T> _elements = new List(2);
int get length => _size;
void add(T e) {
if (_elements.length == _size) {
_resize();
}
_elements[_size] = e;
_size++;
}
void remove(int index) {
for (int i = index + 1; i < _size; i++) {
_elements[i - 1] = _elements[i];
}
_size--;
if (_size == _elements.length / 4) {
_resize();
}
}
void _resize() {
List<T> newElements = new List(_size * 2);
for (int i = 0; i < _size; i++) {
newElements[i] = _elements[i];
}
_elements = newElements;
print("Resized list to size ${_elements.length}");
}
T operator [](int index) => _elements[index];
Iterator get iterator {
return new ArrayListIterator<T>(this);
}
}
class ArrayListIterator<T> extends Iterator<T> {
final ArrayList arrayList;
int position = -1;
ArrayListIterator(this.arrayList);
bool moveNext() {
if (position < arrayList.length - 1) {
position++;
return true;
}
return false;
}
T get current {
return arrayList[position];
}
}