📜  C#中的List.FindIndex()方法与示例

📅  最后修改于: 2021-05-29 23:53:54             🧑  作者: Mango

List .FindIndex方法用于搜索与指定谓词定义的条件匹配的元素,并返回List 中第一次出现的索引。如果找不到符合条件的项目,则此方法将返回-1。此方法的重载列表中有3种方法,如下所示:

  1. FindIndex(Predicate )方法
  2. FindIndex(Int32,Predicate )方法
  3. FindIndex(Int32,Int32,Predicate )方法

FindIndex(Predicate )方法

此方法用于搜索与指定谓词定义的条件匹配的元素,并返回整个List 中第一次出现的从零开始的索引。

返回值:如果找到,则返回匹配match定义的条件的元素的首次出现的索引。如果找不到,则返回-1。

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

例子:

// C# program to demonstrate the use of
// List.FindIndex (Predicate ) method
using System;
using System.Collections.Generic;
  
class GFG1 : IComparable {
  
    public String gg
    {
        get;
        set;
    }
  
    public int CompareTo(Object o)
    {
        GFG1 e = o as GFG1;
        if (e == null)
            throw new ArgumentException("Not valid object");
  
        return gg.CompareTo(e.gg);
    }
}
  
class GFG2 {
  
    String s;
  
    public GFG2(String ss)
    {
        s = ss;
    }
  
    public bool StartsWith(GFG1 e)
    {
        return e.gg.StartsWith(s, StringComparison.InvariantCultureIgnoreCase);
    }
}
  
// Driver Class
class GFG3 {
  
    // Main Method
    public static void Main()
    {
        var e = new List();
  
        // List elements
        e.AddRange(new GFG1[] {
            new GFG1{
                gg = "c",
            },
  
            new GFG1{
                gg = "c++",
            },
            new GFG1{
                gg = "java",
            },
            new GFG1{
                gg = "C#",
            },
            new GFG1{
                gg = "Python",
            },
            new GFG1{
                gg = "Perl",
            },
        });
  
        e.Sort();
        // sorts the list
  
        var es = new GFG2("C");
        Console.WriteLine("'C' starts at index "
                  + e.FindIndex(es.StartsWith));
  
        es = new GFG2("P");
        Console.WriteLine("'P' starts at index " + 
                      e.FindIndex(es.StartsWith));
    }
}
输出:
'C' starts at index 0
'P' starts at index 4

FindIndex(Int32,Predicate )方法

此方法搜索与指定谓词定义的条件匹配的元素,并返回List中从指定索引延伸到最后一个元素的元素范围内第一次出现的索引。

返回值:如果找到,则此方法将返回与“ match”定义的条件相匹配的元素首次出现的索引。如果找不到,则返回-1。

例外情况:

  • ArgumentNullException:如果匹配为null。
  • ArgumentOutOfRangeException:如果startIndex超出List 的有效索引范围。

例子:

// C# program to demonstrate the use of 
// List.FindIndex (Int32, Predicate ) method
using System;
using System.Collections.Generic;
  
class GFG1 : IComparable {
  
    public String gg
    {
        get;
        set;
    }
  
    public int CompareTo(Object o)
    {
        GFG1 e = o as GFG1;
  
        return gg.CompareTo(e.gg);
    }
}
  
class GFG2 {
  
    String s;
  
    public GFG2(String ss)
    {
        s = ss;
    }
  
    public bool StartsWith(GFG1 e)
    {
        return e.gg.StartsWith(s, StringComparison.InvariantCultureIgnoreCase);
    }
}
  
// Driver Class
class GFG3 {
  
    // Main Method
    public static void Main()
    {
        var e = new List();
  
        // List elements
        e.AddRange(new GFG1[] {
            new GFG1{
                gg = "c",
            },
            new GFG1{
                gg = "Python",
            },
            new GFG1{
                gg = "Java",
            },
            new GFG1{
                gg = "C#",
            },
            new GFG1{
                gg = "Java",
            },
            new GFG1{
                gg = "Perl",
            },
        });
  
        // sorts the list
        e.Sort();
         
        var es = new GFG2("C");
  
         // Search for c start from index 1
        Console.WriteLine("Search for 'C' starts at index " 
                          + e.FindIndex(1, es.StartsWith));
         
        es = new GFG2("J");
  
        // search for j starts from index 2
        Console.WriteLine("Search for 'J' starts at index " 
                          + e.FindIndex(2, es.StartsWith));
          
    }
}
输出:
Search for 'C' starts at index 1
Search for 'J' starts at index 2

FindIndex(Int32,Int32,Predicate )方法

此方法搜索与指定谓词定义的条件相匹配的元素,并返回从指定索引开始并包含指定数量的元素的List中元素范围内的第一个从零开始的索引。

返回值:如果找到,则此方法将返回与“ match”定义的条件相匹配的元素首次出现的索引。如果找不到,则返回-1。

例外情况:

  • ArgumentNullException:如果匹配为null。
  • ArgumentOutOfRangeException:如果startIndex超出List 的有效索引范围,或者count小于0或startIndex and count在List 中未指定有效部分。

例子:

// C# program to demonstrate the use of 
// List.FindIndex (Int32, Int32, 
// Predicate ) method
using System;
using System.Collections.Generic;
  
class GFG1 : IComparable {
  
    public String gg
    {
        get;
        set;
    }
  
    public int CompareTo(Object o)
    {
        GFG1 e = o as GFG1;
  
        return gg.CompareTo(e.gg);
    }
}
  
class GFG2 {
  
    String s;
  
    public GFG2(String ss)
    {
        s = ss;
    }
  
    public bool StartsWith(GFG1 e)
    {
        return e.gg.StartsWith(s, StringComparison.InvariantCultureIgnoreCase);
    }
}
  
// Driver Class
class GFG3 {
  
    // Main Method
    public static void Main()
    {
        var e = new List();
  
        // List elements
        e.AddRange(new GFG1[] {
  
            new GFG1{
                gg = "c",
            },
            new GFG1{
                gg = "Python",
            },
            new GFG1{
                gg = "C++",
            },
            new GFG1{
                gg = "Java",
            },
            new GFG1{
                gg = "C#",
            },
            new GFG1{
                gg = "Perl",
            },
        });
  
        // sorts the list
        e.Sort();
          
        var es = new GFG2("C");
  
        // search for c start from index 1
        // count is 2 here
        Console.WriteLine("Search for 'C' starts at index "
                       + e.FindIndex(0, 2, es.StartsWith));
          
        es = new GFG2("J");
  
        // search for j starts from index 2
        // count is 4 here
        Console.WriteLine("Search for 'J' starts at index "
                       + e.FindIndex(2, 4, es.StartsWith));
         
    }
}
输出:
Search for 'C' starts at index 0
Search for 'J' starts at index 3

参考:

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