當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。