列表

初始

List 是一个有序列表

var l = [1, 2, 3];
print(l);

[1, 2, 3]

声明

自动

List<int> l = [];
l
..add(1)
..add(2)
..add(3);
print(l);

[1, 2, 3]

定长

  var l = List<int>.filled(3, 0);
  l[0] = 1;
  l[1] = 2;
  l[2] = 3;
  print(l);

  [1, 2, 3]

生成数据

  List<int> l = List.generate(10, (index) {
    return index;
  });

  print(l);

  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

属性

名称说明
isEmpty是否为空
isNotEmpty是否不为空
first第一个对象
last最后一个对象
length个数
reversed反转
var l = [1, 2, 3];
print(l.first);
print(l.last);
print(l.length);
print(l.isEmpty);
print(l.isNotEmpty);
print(l.reversed);

1
3
3
false
true
(3, 2, 1)

方法

名称说明
add添加
addAll添加多个
insert插入
insertAll插入多个
indexOf查询
indexWhere按条件查询
remove删除
removeAt按位置删除
fillRange按区间填充
getRange按区间获取
shuffle随机变换顺序
sort排序
sublist创建子

添加

List<int> l = [];

l
  ..add(1)
  ..addAll([2, 3, 4, 5])
  ..insert(0, 6)
  ..insertAll(6, [6, 6])
  ;

print(l);

[6, 1, 2, 3, 4, 5, 6, 6]

查询

var l = [6, 1, 2, 3, 4, 5, 6, 6];

print(l.indexOf(5));
print(l.indexWhere((it) => it == 4));

5
4

删除

var l = [6, 1, 2, 3, 4, 5, 6, 6];

l.remove(6);
print(l);
l.removeAt(5);
print(l);

[1, 2, 3, 4, 5, 6, 6]
[1, 2, 3, 4, 5, 6]

Range

var l = [6, 1, 2, 3, 4, 5, 6, 6];

l.fillRange(0, 3, 9);
print(l.getRange(0, 5));

(9, 9, 9, 3, 4)

洗牌

var l = [6, 1, 2, 3, 4, 5, 6, 6];

l.shuffle();
print(l);
l.shuffle();
print(l);

[2, 5, 4, 6, 6, 3, 1, 6]
[6, 4, 6, 5, 2, 3, 6, 1]

排序

  • 数字
var l = [6, 1, 2, 3, 4, 5, 6, 6];

l.sort();
print(l);

[1, 2, 3, 4, 5, 6, 6, 6]
  • 日期
List<DateTime> dtList = [];
dtList.addAll([
  DateTime.now(),
  DateTime.now().add(new Duration(days: -12)),
  DateTime.now().add(new Duration(days: -2))
  ]);
print(dtList);

dtList.sort((a, b) => a.compareTo(b));
print(dtList);

9
[2022-05-29 16:43:17.802, 2022-05-17 16:43:17.802, 2022-05-27 16:43:17.802]
[2022-05-17 16:43:17.802, 2022-05-27 16:43:17.802, 2022-05-29 16:43:17.802]

复制子列表

var l = [6, 1, 2, 3, 4, 5, 6, 6];

var l2 = l.sublist(1,4);
print(l2);

[1, 2, 3]

操作符

名称说明
+连接
[]取值
[]=赋值
var l1 = [1, 2, 3];
var l2 = [4, 5, 6];
print(l1 + l2);
l1[2] = 9;
print(l1[2]);

[1, 2, 3, 4, 5, 6]
9
Last Updated:
Contributors: ducafecat