目录

更新列表(Updating a list)

更新索引

Dart允许修改List中项目的值。 换句话说,可以重写列表项的值。 以下example说明了相同的情况 -

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

上面的示例使用索引0更新List项的值。代码的输出将为 -

[123, 2, 3]

Using the List.replaceRange() function

dart:core库中的List类提供了replaceRange()函数来修改List项。 此函数替换指定范围内的元素的值。

使用List.replaceRange()函数的语法如下 -

List.replaceRange(int start_index,int end_index,Iterable <items>)

Where,

  • Start_index - 表示要开始替换的索引位置的整数。

  • End_index - 表示要停止替换的索引位置的整数。

  • 《items》 - 表示更新值的可迭代对象。

以下example说明了相同的情况 -

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}');
}

它应该产生以下output -

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]
↑回到顶部↑
WIKI教程 @2018