List<T>.RemoveAll(Predicate<T>) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
移除与指定的谓词所定义的条件相匹配的所有元素。
public:
int RemoveAll(Predicate<T> ^ match);
public int RemoveAll (Predicate<T> match);
member this.RemoveAll : Predicate<'T> -> int
Public Function RemoveAll (match As Predicate(Of T)) As Integer
参数
- match
- Predicate<T>
Predicate<T> 委托,用于定义要移除的元素应满足的条件。
返回
从 List<T> 中移除的元素数。
例外
match
为 null
。
示例
以下示例演示 RemoveAll 了 方法以及使用 Predicate<T> 泛型委托的其他几个方法。
创建了一个字符串,其中包含 8 个 List<T> 恐龙名称,其中两个 (位置 1 和 5) 以“saurus”结尾。 该示例还定义了一个名为 EndsWithSaurus
的搜索谓词方法,该方法接受字符串参数并返回一个布尔值,该值指示输入字符串是否以“saurus”结尾。
、 FindFindLast和 FindAll 方法用于使用搜索谓词方法搜索列表。
方法 RemoveAll 用于删除以“saurus”结尾的所有条目。 它从头开始遍历列表,将每个元素依次 EndsWithSaurus
传递到 方法。 如果 EndsWithSaurus
方法返回 true
,则删除 元素。
注意
在 C# 和 Visual Basic 中,无需在 Visual Basic) Predicate(Of String)
中显式创建Predicate<string>
委托 (。 这些语言从上下文中推断出正确的委托,并自动创建它。
最后, Exists 方法验证列表中没有以“saurus”结尾的字符串。
using namespace System;
using namespace System::Collections::Generic;
// Search predicate returns true if a string ends in "saurus".
bool EndsWithSaurus(String^ s)
{
return s->ToLower()->EndsWith("saurus");
};
void main()
{
List<String^>^ dinosaurs = gcnew List<String^>();
dinosaurs->Add("Compsognathus");
dinosaurs->Add("Amargasaurus");
dinosaurs->Add("Oviraptor");
dinosaurs->Add("Velociraptor");
dinosaurs->Add("Deinonychus");
dinosaurs->Add("Dilophosaurus");
dinosaurs->Add("Gallimimus");
dinosaurs->Add("Triceratops");
Console::WriteLine();
for each(String^ dinosaur in dinosaurs )
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
dinosaurs->TrueForAll(gcnew Predicate<String^>(EndsWithSaurus)));
Console::WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs->Find(gcnew Predicate<String^>(EndsWithSaurus)));
Console::WriteLine("\nFindLast(EndsWithSaurus): {0}",
dinosaurs->FindLast(gcnew Predicate<String^>(EndsWithSaurus)));
Console::WriteLine("\nFindAll(EndsWithSaurus):");
List<String^>^ sublist =
dinosaurs->FindAll(gcnew Predicate<String^>(EndsWithSaurus));
for each(String^ dinosaur in sublist)
{
Console::WriteLine(dinosaur);
}
Console::WriteLine(
"\n{0} elements removed by RemoveAll(EndsWithSaurus).",
dinosaurs->RemoveAll(gcnew Predicate<String^>(EndsWithSaurus)));
Console::WriteLine("\nList now contains:");
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs->Exists(gcnew Predicate<String^>(EndsWithSaurus)));
}
/* This code example produces the following output:
Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops
TrueForAll(EndsWithSaurus): False
Find(EndsWithSaurus): Amargasaurus
FindLast(EndsWithSaurus): Dilophosaurus
FindAll(EndsWithSaurus):
Amargasaurus
Dilophosaurus
2 elements removed by RemoveAll(EndsWithSaurus).
List now contains:
Compsognathus
Oviraptor
Velociraptor
Deinonychus
Gallimimus
Triceratops
Exists(EndsWithSaurus): False
*/
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
dinosaurs.TrueForAll(EndsWithSaurus));
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));
Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
dinosaurs.FindLast(EndsWithSaurus));
Console.WriteLine("\nFindAll(EndsWithSaurus):");
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
foreach(string dinosaur in sublist)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\n{0} elements removed by RemoveAll(EndsWithSaurus).",
dinosaurs.RemoveAll(EndsWithSaurus));
Console.WriteLine("\nList now contains:");
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs.Exists(EndsWithSaurus));
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
return s.ToLower().EndsWith("saurus");
}
}
/* This code example produces the following output:
Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops
TrueForAll(EndsWithSaurus): False
Find(EndsWithSaurus): Amargasaurus
FindLast(EndsWithSaurus): Dilophosaurus
FindAll(EndsWithSaurus):
Amargasaurus
Dilophosaurus
2 elements removed by RemoveAll(EndsWithSaurus).
List now contains:
Compsognathus
Oviraptor
Velociraptor
Deinonychus
Gallimimus
Triceratops
Exists(EndsWithSaurus): False
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Compsognathus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Oviraptor")
dinosaurs.Add("Velociraptor")
dinosaurs.Add("Deinonychus")
dinosaurs.Add("Dilophosaurus")
dinosaurs.Add("Gallimimus")
dinosaurs.Add("Triceratops")
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"TrueForAll(AddressOf EndsWithSaurus: {0}", _
dinosaurs.TrueForAll(AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"Find(AddressOf EndsWithSaurus): {0}", _
dinosaurs.Find(AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"FindLast(AddressOf EndsWithSaurus): {0}", _
dinosaurs.FindLast(AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"FindAll(AddressOf EndsWithSaurus):")
Dim sublist As List(Of String) = _
dinosaurs.FindAll(AddressOf EndsWithSaurus)
For Each dinosaur As String In sublist
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"{0} elements removed by RemoveAll(AddressOf EndsWithSaurus).", _
dinosaurs.RemoveAll(AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & "List now contains:")
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Exists(AddressOf EndsWithSaurus): {0}", _
dinosaurs.Exists(AddressOf EndsWithSaurus))
End Sub
' Search predicate returns true if a string ends in "saurus".
Private Shared Function EndsWithSaurus(ByVal s As String) _
As Boolean
Return s.ToLower().EndsWith("saurus")
End Function
End Class
' This code example produces the following output:
'
'Compsognathus
'Amargasaurus
'Oviraptor
'Velociraptor
'Deinonychus
'Dilophosaurus
'Gallimimus
'Triceratops
'
'TrueForAll(AddressOf EndsWithSaurus: False
'
'Find(AddressOf EndsWithSaurus): Amargasaurus
'
'FindLast(AddressOf EndsWithSaurus): Dilophosaurus
'
'FindAll(AddressOf EndsWithSaurus):
'Amargasaurus
'Dilophosaurus
'
'2 elements removed by RemoveAll(AddressOf EndsWithSaurus).
'
'List now contains:
'Compsognathus
'Oviraptor
'Velociraptor
'Deinonychus
'Gallimimus
'Triceratops
'
'Exists(AddressOf EndsWithSaurus): False
注解
Predicate<T>是方法的true
委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回该方法。 当前 List<T> 元素单独传递给 Predicate<T> 委托,与 条件匹配的元素将从 List<T>中删除。
此方法执行线性搜索;因此,此方法是 O (n) 运算,其中 n 为 Count。