Created
November 21, 2018 08:14
-
-
Save CaiJingLong/1174367a07c474b06837e88c6014db3a to your computer and use it in GitHub Desktop.
一个自定义的dart list 示例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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