Dart07List
Dart07List
列表可分为两种:固定长度列表和可增长列表
固定长度列表
示例
void main() {
var lst = new List(3);
lst[0] = 12;
lst[1] = 13;
lst[2] = 11;
print(lst);
}
可增长列表
var list_name = [val1,val2,val3] // 创建包含指定值的列表
var list_name = new List() // 创建一个大小为零的列表
列表属性
下表列出了dart:core 库中 List 类的一些常用属性。
属性 | 描述 |
---|---|
first | 返回第一个元素。 |
isEmpty | 如果集合没有元素,则返回true。 |
isNotEmpty | 如果集合至少包含一个元素,则返回true。 |
length | 返回列表的大小(元素数量)。 |
last | 返回列表中的最后一个元素。 |
reversed | 以相反的顺序返回包含列表值的可迭代对象。 |
single | 检查列表是否只有一个元素并返回它。 |
添加元素
可变列表可以在运行时动态增长,List.add() 函数将指定的值附加到 List 的末尾并返回修改后的 List 对象。
void main() {
List l = [1,2,3];
l.add(12);
print(l);
}
它将产生以下输出
[1, 2, 3, 12]
List.addAll() 函数接受以逗号分隔的多个值,并将这些值附加到 List。
void main() {
List l = [1,2,3];
l.addAll([12,13]);
print(l);
}
它将产生以下输出
[1, 2, 3, 12, 13]
Dart 还支持在 List 中的特定位置添加元素。insert() 函数接受一个值并将其插入指定的索引。类似地,insertAll() 函数从指定的索引开始插入给定的值列表。
List.insert(index,value)
List.insertAll(index, iterable_list_of _values)
示例:List.insert()
void main() {
List l = [1,2,3];
l.insert(0,4);
print(l);
}
执行上面示例代码,得到以下结果:
[120, 130, 1, 2, 3]
更新列表
Dart 可以修改 List 中项目的值。换句话说,可以重写列表项的值。如下所示:
void main() {
List l = [1, 2, 3,];
1[0] = 123;
print (1);
}
上面的示例更新List中索引为0项的值。执行上面示例代码,得到以下结果:
[123, 2, 3]
使用List.replaceRange() 函数
dart:core 库中的 List 类提供了 replaceRange() 函数来修改List项。此函数替换指定范围内元素的值。
使用 List.replaceRange() 函数的语法如下:
List.replaceRange(int start_index,int end_index,Iterable <items>)
其中,
- start_index - 表示要开始替换的索引位置的整数。
- end_index - 表示要停止替换的索引位置的整数。
- <items> - 表示更新值的可迭代对象。
参考以下示例代码:
void main() {
List l = [1, 2, 3,4,5,6,7,8,9];
print('The value of list before replacing ${l}');
l.replaceRange(0,3,[11,23,24]);
print('The value of list after replacing the items between the range [0-3] is ${l}');
}
它应该产生以下输出:
The value of list before replacing [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after replacing the items between the range [0-3] is [11, 23, 24, 4, 5, 6, 7, 8, 9]
删除列表项
List.remove() 函数删除列表中第一次出现的指定项。如果成功地从列表中删除指定的值,则此函数返回 true。
List.remove(Object value) // value - 表示要从列表中删除的项的值。
示例:
void main() {
List l = [1, 2, 3,4,5,6,7,8,9];
print('The value of list before removing the list element ${l}');
bool res = l.remove(1);
print('The value of list after removing the list element ${l}');
}
它将产生以下输出:
The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after removing the list element [2, 3, 4, 5, 6, 7, 8, 9]
List.removeAt() 函数删除指定索引处的值并返回它。
List.removeAt(int index) // index - 表示应从列表中删除的元素的索引。
以下示例显示如何使用此功能:
void main() {
List l = [1, 2, 3,4,5,6,7,8,9];
print('The value of list before removing the list element ${l}');
dynamic res = l.removeAt(1);
print('The value of the element ${res}');
print('The value of list after removing the list element ${l}');
}
执行上面示例代码,得到以下结果:
List.removeLast() 函数弹出并返回List中的最后一项。语法如下:
以下示例显示如何使用此函数
void main() {
List l = [1, 2, 3,4,5,6,7,8,9];
print('The value of list before removing the list element ${l}');
dynamic res = l.removeLast();
print('The value of item popped ${res}');
print('The value of list after removing the list element ${l}');
}
执行上面示例代码,得到以下结果:
The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of item popped 9
The value of list after removing the list element [1, 2, 3, 4, 5, 6, 7, 8]
List.removeRange() 函数删除指定范围内的项目。语法如下
List.removeRange(int start, int end)
其中,
- start - 表示删除项目的起始位置。
- end - 表示列表中停止删除项目的位置。
以下示例显示如何使用此函数:
void main() {
List l = [1, 2, 3,4,5,6,7,8,9];
print('The value of list before removing the list element ${l}');
l.removeRange(0,3);
print('The value of list after removing the list
element between the range 0-3 ${l}');
}
执行上面示例代码,得到以下结果:
The value of list before removing the list element
[1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after removing the list element
between the range 0-3 [4, 5, 6, 7, 8, 9]