目录

将元素插入列表(Inserting Elements into a List)

可变列表可以在运行时动态增长。 List.add()函数将指定的值附加到List的末尾并返回修改后的List对象。 下面说明了相同的内容。

void main() { 
   List l = [1,2,3]; 
   l.add(12); 
   print(l); 
}

它将产生以下output -

[1, 2, 3, 12]

List.addAll()函数接受以逗号分隔的多个值,并将这些值附加到List。

void main() { 
   List l = [1,2,3]; 
   l.addAll([12,13]); 
   print(l); 
}

它将产生以下output -

[1, 2, 3, 12, 13]

List.addAll()函数接受以逗号分隔的多个值,并将这些值附加到List。

void main() { 
   List l = [1,2,3]; 
   l.addAll([12,13]); 
   print(l); 
} 

它将产生以下output -

[1, 2, 3, 12, 13]

Dart还支持在List中的特定位置添加元素。 insert()函数接受一个值并将其插入指定的索引。 类似地, insertAll()函数从指定的索引开始插入给定的值列表。 insert和insertAll函数的语法如下所示 -

List.insert(index,value) 
List.insertAll(index, iterable_list_of _values)

以下示例分别说明了insert()insertAll()函数的insertAll()

语法 (Syntax)

List.insert(index,value)  
List.insertAll([Itearble])

Example : List.insert()

void main() { 
   List l = [1,2,3]; 
   l.insert(0,4); 
   print(l); 
}

它将产生以下output -

[4, 1, 2, 3]

Example : List.insertAll()

void main() { 
   List l = [1,2,3]; 
   l.insertAll(0,[120,130]); 
   print(l); 
}

它将产生以下output -

[120, 130, 1, 2, 3]
↑回到顶部↑
WIKI教程 @2018