可变列表可以在运行时动态增长, List.add()函数将指定的值附加到List的末尾,并返回修改后的List对象 ,如下所示。
void main() { List l=[1,2,3]; l.add(12); print(l); }
它将产生以下输出-
[1, 2, 3, 12]
List.addAll()函数接受多个用逗号分隔的值,并将它们附加到列表中。
void main() { List l=[1,2,3]; l.addAll([12,13]); print(l); }
它将产生以下输出-
[1, 2, 3, 12, 13]
List.addAll()函数接受多个用逗号分隔的值,并将它们附加到列表中。
void main() { List l=[1,2,3]; l.addAll([12,13]); print(l); }
它将产生以下输出-
[1, 2, 3, 12, 13]
Dart还支持在列表中的特定位置添加元素, insert()函数接受一个值并将其插入指定的索引处,同样, insertAll()函数从指定的索引开始插入给定的值列表。 insert和 insertAll 函数的语法如下所示-
List.insert(index,value) List.insertAll(index, iterable_list_of _values)
以下示例分别说明了 insert()和 insertAll()函数的用法。
语法
List.insert(index,value) List.insertAll([Itearble])
示例 - List.insert()
void main() { List l=[1,2,3]; l.insert(0,4); print(l); }
它将产生以下输出-
[4, 1, 2, 3]
示例 - List.insertAll()
void main() { List l=[1,2,3]; l.insertAll(0,[120,130]); print(l); }
它将产生以下输出-
[120, 130, 1, 2, 3]
参考链接
https://www.learnfk.com/dart-programming/dart-programming-inserting-elements-into-list.html
标签:Inserting,insert,Elements,into,List,12,insertAll,print,main From: https://blog.51cto.com/u_14033984/8401093