当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# List.FindIndex()用法及代码示例


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

    • FindIndex(Predicate)方法
    • FindIndex(Int32,Predicate)方法
    • FindIndex(Int32,Int32,Predicate)方法

FindIndex(Predicate)方法

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


用法: public int FindIndex (Predicate<T> match);

参数:
match:谓词委托定义了要搜索的元素的条件。

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

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

例:

// C# program to demonstrate the use of 
// List<T>.FindIndex (Predicate <T>) 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<GFG1>(); 
  
        // 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中从指定索引延伸到最后一个元素的元素范围内第一次出现的索引。

用法: public int FindIndex (int startIndex, Predicate<T> match);

参数:
startIndex:它是搜索的从零开始的索引。
match:谓词委托定义了要搜索的元素的条件。

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


异常:

  • ArgumentNullException:如果匹配为空。
  • ArgumentOutOfRangeException:如果startIndex在List的有效索引范围之外。

例:

// C# program to demonstrate the use of  
// List<T>.FindIndex (Int32, Predicate <T>) 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<GFG1>(); 
  
        // 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中元素范围内的首次出现的从零开始的索引。

用法: public int FindIndex (int startIndex, int count, Predicate<T> match);

参数:
startIndex:它是搜索的从零开始的索引。
count:它是要搜索的部分中元素的数量。
match:谓词委托定义了要搜索的元素的条件。

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

异常:

  • ArgumentNullException:如果匹配为空。
  • ArgumentOutOfRangeException:如果startIndex不在列表的有效索引范围内,或者count小于0,或者startIndex和count在List中未指定有效部分。

例:

// C# program to demonstrate the use of  
// List<T>.FindIndex (Int32, Int32,  
// Predicate <T>) 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<GFG1>(); 
  
        // 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

参考:



相关用法


注:本文由纯净天空筛选整理自SoumikMondal大神的英文原创作品 List.FindIndex() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。