List<T>.RemoveAll(Predicate<T>) 메서드

정의

지정된 조건자에 정의된 조건과 일치하는 요소를 모두 제거합니다.Removes all the elements that match the conditions defined by the specified predicate.

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> 대리자입니다.The Predicate<T> delegate that defines the conditions of the elements to remove.

반환

Int32

List<T>에서 제거한 요소의 수입니다.The number of elements removed from the List<T>.

예외

match이(가) null인 경우match is null.

예제

다음 예제에서는 RemoveAll 메서드 및 제네릭 대리자를 사용 하는 몇 가지 다른 메서드를 보여 줍니다 Predicate<T> .The following example demonstrates the RemoveAll method and several other methods that use the Predicate<T> generic delegate.

List<T>문자열의가 만들어지고,이 중 두 개의 공룡 이름 (위치 1과 5)은 "saurus"로 끝납니다.A List<T> of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". 또한이 예제에서는 EndsWithSaurus 문자열 매개 변수를 허용 하 고 입력 문자열이 "saurus"로 끝나는지 여부를 나타내는 부울 값을 반환 하는 라는 검색 조건자 메서드를 정의 합니다.The example also defines a search predicate method named EndsWithSaurus, which accepts a string parameter and returns a Boolean value indicating whether the input string ends in "saurus".

Find, FindLast 및 메서드는 검색 조건자 메서드를 사용 하 여 FindAll 목록을 검색 하는 데 사용 됩니다.The Find, FindLast, and FindAll methods are used to search the list with the search predicate method.

RemoveAll메서드는 "saurus"로 끝나는 모든 항목을 제거 하는 데 사용 됩니다.The RemoveAll method is used to remove all entries ending with "saurus". 처음부터 목록을 트래버스 하 고 각 요소를 EndsWithSaurus 메서드에 전달 합니다.It traverses the list from the beginning, passing each element in turn to the EndsWithSaurus method. 메서드가를 반환 하는 경우 요소가 제거 됩니다 EndsWithSaurus true .The element is removed if the EndsWithSaurus method returns true.

참고

C # 및 Visual Basic에서는 Predicate<string> Predicate(Of String) Visual Basic에서 명시적으로 대리자를 만들 필요가 없습니다.In C# and Visual Basic, it is not necessary to create the Predicate<string> delegate (Predicate(Of String) in Visual Basic) explicitly. 이러한 언어는 컨텍스트에서 올바른 대리자를 유추 하 고 자동으로 만듭니다.These languages infer the correct delegate from context, and create it automatically.

마지막으로 Exists 메서드는 "saurus"로 끝나는 문자열이 목록에 없는지 확인 합니다.Finally, the Exists method verifies that there are no strings in the list that end with "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 전달 된 개체가 대리자에 정의 된 조건과 일치 하는 경우를 반환 하는 메서드에 대 한 대리자입니다.The Predicate<T> is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. 현재의 요소는 List<T> 개별적으로 대리자에 전달 되 Predicate<T> 고 조건과 일치 하는 요소는에서 제거 됩니다 List<T> .The elements of the current List<T> are individually passed to the Predicate<T> delegate, and the elements that match the conditions are removed from the List<T>.

이 메서드는 선형 검색을 수행 합니다. 따라서이 메서드는 O (n) 작업입니다. 여기서 nCount 입니다.This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

적용 대상

추가 정보