(1)dart中没有严格区分可变集合和不可变集合。
(2)Dart中可以直接打印List内的元素,而不是像Java打印List对象的地址。
(3)List集合的下标是从0开始
(4) 支持泛型
方式1:直接使用[]初始化
List<int> ageList = [17, 18, 19, 30];
方式2:带泛型声明
var ageList = <int> [17, 18, 19, 30];
方式3:实例化定长List对象
List<int> ageList = List(14); //初始化后集合里面的内容全为空,一旦指定了集合长度,不能再调用add方法
方式4:实例化动态增长List对象
List<int> ageList = List(); // 相当于 List<int> ageList = []; 初始化完之后,不能直接ageList[3]=12,这样会报错。应该使用ageList.add(11);
方式1:for for(var i = 0; i < ageList.length; i++){...} 方式2:forEach ageList.forEach((age){...}) 方式3:for-in for(var age in ageList) {...} 方式4:iterator迭代器 while(ageList.iterator.moveNext()) { ageList.iterator.current }
- part of dart.core;
- abstract class List<E> implements EfficientLengthIterable<E> {
- external factory List([int? length]);
- external factory List.filled(int length, E fill, {bool growable = false});
- @Since("2.8")
- external factory List.empty({bool growable = false});
- external factory List.from(Iterable elements, {bool growable = true});
- external factory List.of(Iterable
elements, {bool growable = true}); - external factory List.generate(int length, E generator(int index),{bool growable = true});
- external factory List.unmodifiable(Iterable elements);
- static List<T> castFrom<S, T>(List
source) => CastList(source); - static void copyRange<T>(List
target, int at, List source, - [int? start, int? end]) {
- start ??= 0;
- end = RangeError.checkValidRange(start, end, source.length);
- if (end == null) {
- // TODO(dart-lang/language#440): Remove when promotion works.
- throw "unreachable";
- }
- int length = end - start;
- if (target.length < at + length) {
- throw ArgumentError.value(target, "target",
- "Not big enough to hold $length elements at position $at");
- }
- if (!identical(source, target) || start >= at) {
- for (int i = 0; i < length; i++) {
- target[at + i] = source[start + i];
- }
- } else {
- for (int i = length; --i >= 0;) {
- target[at + i] = source[start + i];
- }
- }
- }
- static void writeIterable<T>(List
target, int at, Iterable source ) { - RangeError.checkValueInInterval(at, 0, target.length, "at");
- int index = at;
- int targetLength = target.length;
- for (var element in source) {
- if (index == targetLength) {
- throw IndexError(targetLength, target);
- }
- target[index] = element;
- index++;
- }
- }
- List<R> cast<R>();
- E operator [](int index);
- void operator []=(int index, E value);
- void set first(E value);
- void set last(E value);
- int get length;
- set length(int newLength);
- void add(E value);
- void addAll(Iterable
iterable ); - Iterable
get reversed; //倒序过来并返回Iterable - void sort([int compare(E a, E b)?]); //排序
- void shuffle([Random? random]); //将list重新洗牌
- int indexOf(E element, [int start = 0]);//查找列表中某对象的索引,第二个参数是从第几开始计数
- int indexWhere(bool test(E element), [int start = 0]);//查找列表中某对象的索引,从前往后找,第二个参数是从第几开始计数
- int lastIndexWhere(bool test(E element), [int? start]);//查找列表中某对象的索引,从后往前找,第二个参数是从第几开始计数
- int lastIndexOf(E element, [int? start]);
- void clear();
- void insert(int index, E element);// 从指定位置插入
- void insertAll(int index, Iterable
iterable ); - void setAll(int index, Iterable
iterable ); - bool remove(Object? value);
- E removeAt(int index);//移除集合中第index+1个元素
- E removeLast();
- void removeWhere(bool test(E element)); //按条件删除集合中的元素
- void retainWhere(bool test(E element)); //按条件保留结合中的元素
- List
operator +(List other); //两个集合相加操作 - List
sublist(int start, [int? end]) ;//截取某段 - Iterable
getRange(int start, int end) ; //截取某段,[a,b) - void setRange(int start, int end, Iterable
iterable, [int skipCount = 0] ); - void removeRange(int start, int end);
- void fillRange(int start, int end, [E? fillValue]);
- void replaceRange(int start, int end, Iterable
replacement ); - Map<int, E> asMap();
- bool operator ==(Object other);
- }
排序sort:
- List
list = new List(); - Student stu1 = new Student(19,'小孙');
- Student stu2 =new Student(9,'小王');
- Student stu3 = new Student(12,'小李');
- Student stu4 = new Student(11,'小郭');
- list.add(stu1);
- list.add(stu2);
- list.add(stu3);
- list.add(stu4);
- print(list.first.name);
- print(list.elementAt(1).name);
- print(list.elementAt(2).name);
- print(list.last.name);
- list.sort((a,b)=>b.age.compareTo(a.age)); //a和b可以自己任意定义,如果是a.compareTo(b) 升序,如果是b.compareTo(a) 降序
- print("==============分割线==================");
- print(list.first.name);
- print(list.elementAt(1).name);
- print(list.elementAt(2).name);
- print(list.last.name);
定位索引indexOf:
print(list.indexOf(stu2, 0)); // 第一个参数是Studuent对象,第二个参数是从自定义的参数开始
定位索引indexWhere:
print(list.indexWhere((e) => e.age == 11, 0));
按条件保留retainWhere:
list.retainWhere((element) => element.age == 11);