📌  相关文章
📜  C#|删除列表中与谓词定义的条件匹配的所有元素

📅  最后修改于: 2021-05-29 17:03:00             🧑  作者: Mango

List .RemoveAll(Predicate )方法用于删除所有与指定谓词定义的条件匹配的元素。

列表的属性:

  • 它与数组不同。列表可以动态调整大小,但数组则不能。
  • 列表类可以接受null作为引用类型的有效值,并且还允许重复的元素。
  • 如果计数等于容量,则列表的容量将通过重新分配内部数组而自动增加。在添加新元素之前,现有元素将被复制到新数组。

句法:

public int RemoveAll (Predicate match);

范围:

返回值:该方法返回要从List 中删除的元素数。

异常:如果匹配为null,则此方法将提供ArgumentNullException。

下面的程序说明了List .RemoveAll(Predicate )方法的用法:

范例1:

// C# Program to remove all elements of
// a List that match the conditions
// defined by the predicate
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // function which checks whether an
    // element is even or not. Or you can
    // say it is the specified condition
    private static bool isEven(int i)
    {
        return ((i % 2) == 0);
    }
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        for (int i = 1; i <= 10; i++) {
            firstlist.Add(i);
        }
  
        Console.WriteLine("Elements Present in List:\n");
  
        // Displaying the elements of List
        foreach (int k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.Write("Number of Elements Removed: ");
  
        // Removing the elements which is even
        // This will return 5 as it removed 5
        // even elements from the list
        Console.WriteLine(firstlist.RemoveAll(isEven));
  
        Console.WriteLine(" ");
  
        Console.WriteLine("Remaining Elements in List:");
  
        // Displaying the elements of List
        foreach (int k in firstlist)
        {
            Console.WriteLine(k);
        }
    }
}

输出:

Elements Present in List:

1
2
3
4
5
6
7
8
9
10
 
Number of Elements Removed: 5
 
Remaining Elements in List:
1
3
5
7
9

范例2:

// C# Program to remove all elements of
// a List that match the conditions
// defined by the predicate
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // function which checks whether an
    // element is even or not. Or you can
    // say it is the specified condition
    private static bool isEven(int i)
    {
        return ((i % 2) == 0);
    }
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding items to List
        firstlist.Add(13);
        firstlist.Add(17);
        firstlist.Add(19);
        firstlist.Add(11);
  
        Console.WriteLine("Elements Present in List:\n");
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.Write("Number of Elements Removed: ");
  
        // Removing the elements which is even
        // This will return 0 as it no elements
        // are even in List
        Console.WriteLine(firstlist.RemoveAll(isEven));
  
        Console.WriteLine(" ");
  
        Console.WriteLine("Remaining Elements in List:");
  
        // Displaying the elements of List
        foreach (int k in firstlist)
        {
            Console.WriteLine(k);
        }
    }
}

输出:

Elements Present in List:

13
17
19
11
 
Number of Elements Removed: 0
 
Remaining Elements in List:
13
17
19
11

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.removeall?view=netframework-4.7.2