Dart list remove()


dart:core库中List类支持的以下函数可用于删除List中的项目。

List.remove()

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 函数删除指定索引处的值并将其返回。

句法

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

它将产生以下输出

The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of the element 2
The value of list after removing the list element [1, 3, 4, 5, 6, 7, 8, 9]

List.removeLast()

List.removeLast() 函数持久性有机污染物,并在返回列表中的最后一个项目。相同的语法如下 -

List.removeLast()

以下 示例 显示如何使用此功能

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() 函数删除指定范围内的物品。相同的语法如下 -

List.removeRange(int start, int 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]