数组List
声明变量
//声明时直接赋值
List list1 = ['a', 'b', 'c'];
List list2 = [1, 2, 3, 4, 5, 6];
//先声明再赋值,new在dart中可以省略
List list3 = new List();
list3.add('3');
list3.add(100);
print(list1); //[a, b, c]
print(list2); //[1, 2, 3, 4, 5, 6]
print(list3); //[3, 100]
属性
List list = ['apple', 'banana', 'peach', 'tomato'];
//数组长度
print(list.length); //4
//数组反转、颠倒
print(list.reversed); //(tomato, peach, banana, apple)
//数组头元素和尾元素
print(list.first); //apple
print(list.last); //tomato
//数组是否为空
print(list.isEmpty); //false
print(list.isNotEmpty); //true
//类型
print(list.runtimeType); //List<dynamic>
//迭代器
Iterator itr = list.iterator;
while (itr.moveNext()) { //下面有值返回true,否则返回false
print(itr.current);//apple banana peach tomato
}
//检测数组单元素
List list = ['apple'];
//数组是单元素就返回元素值,否则报错
print(list.single);
方法
数组增加元素
List list = ['apple', 'banana', 'peach', 'tomato'];
//尾部增加单个元素
list.add('strawberry');
print(list); //[apple, banana, peach, tomato, strawberry]
//尾部增加多个元素
list.addAll(['new1', 'new2']);
print(list);//[apple, banana, peach, tomato, new1, new2]
//插入单个值
list.insert(2, 'strawberry');
print(list);//[apple, banana, strawberry, peach, tomato]
//插入多个值
list.insertAll(2, ['new1', 'new2']);
print(list);//[apple, banana, new1, new2, peach, tomato]
数组删除元素
List list = ['apple', 'banana', 'peach', 'tomato'];
//删除单个指定元素
list.remove('apple');
print(list);//[banana, peach, tomato]
//删除尾部单个元素
list.removeLast();
print(list);//[apple, banana, peach]
//删除单个指定索引值元素
list.removeAt(2);
print(list);//[apple, banana, tomato]
//删除范围索引值内的元素
list.removeRange(0, 2);
print(list);//[peach, tomato]
//删除匹配的值
list.removeWhere((element) => element == 'banana');
print(list);//[apple, peach, tomato]
//删除所有数组里的值
list.clear();
print(list);//[]
数组数据类型转换
List list = ['apple', 'banana', 'peach', 'tomato'];
//转字符串
print(list.join(','));//apple,banana,peach,tomato
print(list.toString());//[apple, banana, peach, tomato]
//转集合
print(list.toSet());//{apple, banana, peach, tomato}
//转字典
print(list.asMap());//{0: apple, 1: banana, 2: peach, 3: tomato}
数组遍历
List list = ['apple', 'banana', 'peach', 'tomato'];
//forEach 遍历List中的每一个元素
list.forEach((element) {
print(element);//apple banana peach tomato
});
//map map遍历List中的每一个元素,map会返回一个新的List对象。
Iterable res = list.map((value) {
return value;
});
print(res);
//reduce fold 依次访问List中所有的元素,并将List中所有的元素根据传入的函数压缩成一个数值
List list = [1, 2, 3, 4];
var res = list.reduce((a, b) => a * b);
print(res);//24
var res2 = list.fold(10, (a, b) => a * b);
print(res2);
//expand 遍历List中每一个元素,根据传入函数将所遍历元素扩展。
var res3 = list.expand((element) => [element, element + 1]);
print(res3);// (1, 2, 2, 3, 3, 4, 4, 5)
数组匹配
List list = [1, 2, 3, 4];
//every 所有值符合函数返回true,否则返回false
bool res = list.every((element) => element > 2);
print(res); //flase
//any 所有值其中有一个值符合返回true,否则返回false
bool res2 = list.any((element) => element > 2);
print(res2); //true
//where 返回所有符合函数的值
var res3 = list.where((element) => element > 2);
print(res3); //(3,4)
//firstWhere 返回从左到右第一个符合函数的值
var res6 = list.firstWhere((element) => element > 2);
print(res6); //3
//lastWhere 返回从右到左第一个符合函数的值
var res7 = list.lastWhere((element) => element > 2);
print(res7); //4
//返回从左到右第一个符合函数的值的索引值
var res4 = list.indexWhere((element) => element > 2); //返回index:2
print(res4); //2
//返回从右到左第一个符合函数的值的索引值
var res5 = list.lastIndexWhere((element) => element > 2);
print(res5); //3
//只有一个值符合函数返回其值,否则报错
var res8 = list.singleWhere((element) => element > 3);
print(res8); //4
// 保留满足条件的元素
list.retainWhere((element) => element > 2);
list.join(',');
print(list); //[3,4]
//从混合类型的List中,筛选出指定类型的数据
List list = [1, 2, 'a', 4];
Iterable<int> res = list.whereType();
print(res);//(1, 2, 4)
Iterable<String> res2 = list.whereType();
print(res2);//(a)
数组取值与略值
//getRange取值
List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
Iterable<String> range = colors.getRange(1, 4);
print(range.join(', '));//green, blue, orange
//elementAt按索引值取单个值
List list = [1, 2, 3, 4];
print(list.elementAt(2));//3
//indexOf从左到右indexOf取索引值
List list = [1, 2, 3, 4, 3];
print(list.indexOf(3));//2
//lastIndexOf从右到左indexOf取索引值
List list = [1, 2, 3, 4, 3];
print(list.lastIndexOf(3));//0
//sublist按索引值取多个值
List list = [1, 2, 3, 4];
print(list.sublist(0, 3));//[1, 2, 3]
//取前两位
print(list.take(2));
//取满足函数表达式条件的值
print(list.takeWhile((value) => value < 4));
List list = [1, 2, 3, 4];
//跳过前两位
print(list.skip(2));
//跳过满足函数表达式条件
print(list.skipWhile((value) => value < 4));
数组打乱和整理
List list = [1, 2, 3, 4];
//打乱
list.shuffle();
print(list);//[2, 3, 1, 4]
//整理
List list = [2, 1, 3, 4];
list.sort();
print(list);//[1, 2, 3, 4]
数组替换、填充值
//setRange元素替换
List list1 = [1, 2, 3, 4];
List list2 = [5, 6];
list1.setRange(0, 2, list2);//将list2的元素替换list1的第一个和第二个元素0-2
print(list1);//[5, 6, 3, 4]
//replaceRange元素替换
List list = [1, 2, 3, 4];
list.replaceRange(0, 2, [5, 6]);
print(list);//[5, 6, 3, 4]
//setAll多个值替换
List<String> list = ['a', 'b', 'c'];
list.setAll(1, ['bee', 'sea']);
list.join(', '); // 'a, bee, sea'
print(list);
//fillRange填充值
List<int> list2 = new List(3);
list2.fillRange(0, 2, 1);
print(list2); // [1, 1, null]
//followedBy后接数组
List list = [1, 2, 3, 4];
List lsit2 = [5, 6];
print(list.followedBy(lsit2));//(1, 2, 3, 4, 5, 6)
检测数组
//contains检测关键值是否在数组里面
List list = [1, 2, 3, 4];
print(list.contains(2));//true