Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created November 21, 2018 08:14
Show Gist options
  • Save CaiJingLong/1174367a07c474b06837e88c6014db3a to your computer and use it in GitHub Desktop.
Save CaiJingLong/1174367a07c474b06837e88c6014db3a to your computer and use it in GitHub Desktop.
一个自定义的dart list 示例
import 'dart:collection';
class MyList<E> extends Object with ListMixin<E> implements List<E> {
var _array = <E>[];
@override
int get length => _array.length;
@override
void set length(int newLength) => _array.length = newLength;
@override
E operator [](int index) {
return _array[index];
}
@override
void operator []=(int index, E value) {
_array[index] = value;
}
void insertFirst(E e) {
this.insert(0, e);
}
}
main(List<String> arguments) async {
var myList = MyList<int>();
myList.add(1);
myList.add(2);
myList.add(3);
myList.add(4);
myList.insertFirst(0);
print(myList);
print(myList.length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment