📜  在C#中列出FindLastIndex()方法。设置-2

📅  最后修改于: 2021-05-29 22:55:22             🧑  作者: Mango

此方法用于搜索与指定谓词定义的条件匹配的元素,并返回List 或其一部分中最后一次出现的从零开始的索引。此方法的重载列表中有3种方法:

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

在这里,我们将仅讨论最后两种方法。

FindLastIndex(Int32,Predicate )方法

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

返回值:如果找到该元素,则它将返回最后一次出现的,通过参数“ match匹配指定条件的元素的Int32类型的从零开始的索引。如果找不到,则返回“ -1 ”。

例外情况:

  • ArgumentNullException:如果匹配null
  • ArgumentOutOfRangeException:如果开始位置超出List 的有效索引范围。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# program to illustrate the 
// List.FindLastIndex(Int32,
// Predicate ) Method
using System;
using System.Collections.Generic;
  
class GFG {
      
    // Main Method
    public static void Main()
    {
        // List creation
        // List name is "PC"
        List PC = new List();
  
        // elements in the List
        PC.Add("Computer");
        PC.Add("keyboard");
        PC.Add("laptop");
        PC.Add("mouse");
  
        // the search will starts from index 2
        int indx = PC.FindLastIndex(2, FindIndex);
          
        Console.WriteLine(indx);
    }
  
    // Conditional method
    private static bool FindIndex(string g)
    {
  
        if (g == "Computer") 
        {
            return true;
        }
          
        else 
        {
            return false;
        }
    }
}

输出:

0

范例2:

// C# program to illustrate the 
// List.FindLastIndex(Int32,
// Predicate ) Method
using System;
using System.Collections.Generic;
  
class GFG {
      
    // Main Method
    public static void Main()
    {
          
        // List creation
        // List name is "PC"
        List PC = new List();
  
        // elements in the List
        PC.Add(3);
        PC.Add(4);
        PC.Add(5);
        PC.Add(6);
  
        // condition is "FindIndex"
        int indx = PC.FindLastIndex(2, FindIndex);
  
        Console.WriteLine(indx);
    }
  
    // Conditional method
    private static bool FindIndex(int g)
    {
          
        // search for "5"
        if (g == 5)
          
        {
            return true;
        }
          
        else 
        {
            return false;
        }
    }
}

输出:

2

示例3:在此示例中,我们使用XML文件并从起始索引中搜索项目,然后打印该项目的索引,如果未找到该项目,则打印“ -1”,如果找到,则打印索引。该项目是“ GeeksForGeeks ”。但是这里我们没有XML文件,这里编译器给出了一个例外。

// C# program to illustrate the 
// List.FindLastIndex(Int32,
// Predicate ) Method
using System;
using System.Collections.Generic;
using System.Linq;
  
class GFG {
  
    // here List contains the object "gfg" using
    // data from a sample XML file
    // List initialize
    private static List geeks = new List();
  
    public static void Main()
    {
  
        // if the item is found then
        // it prints the index
        // if not found prints "-1"
        int x = geeks.FindLastIndex(3, FindGFG);
        Console.WriteLine(x);
          
    }
  
    // conditional method
    private static bool FindGFG(gfg g)
    {
  
         // item is "GeeksForGeeks"
        if (g.G == "GeeksForGeeks")
        {
            return true;
        }
  
        else
        {
            return false;
        }
    }
}
  
public class gfg {
      
    public string G
    {
        get;
        set;
    }
}

运行时错误:

FindLastIndex(Int32,Int32,Predicate )方法

此方法搜索与指定谓词定义的条件匹配的元素,并返回整个List中最后一次出现的从零开始的索引,并且该列表包含指定数量的元素并在指定索引处结束。

返回值:如果找到该元素,则它通过参数“ match”返回与指定条件匹配的最后一个元素的Int32类型的从零开始的索引。如果未找到,则返回“ -1”。

例外情况:

  • ArgumentNullException:如果“ match ”为null。
  • ArgumentOutOfRangeException:如果“ startIndex”超出范围或“ count”小于0(零),或者“ startIndex”和“ count”未在列表中指定有效部分

例子:

// C# Program to illustrate the 
// FindLastIndex(Int32, Int32, 
// Predicate) Method
using System;
using System.Collections.Generic;
    
class GFG
{
    public static void Main()
    {
        // List name is "mylist"
        List mylist = new List();
            
        // Elements in the List
        mylist.Add("C");
        mylist.Add("C++");
        mylist.Add("Java");
        mylist.Add("Python");
        mylist.Add("C#");
        mylist.Add("HTML");
        mylist.Add("Java");
        mylist.Add("PHP");
            
        // the search will starts from index 2
        // the number of element is 3
        int indx = mylist.FindLastIndex(2, 3, FindIndex);
          
        Console.WriteLine("The index of Java is: "+indx);
    }
        
    // Conditional method
    private static bool FindIndex(string g)
    {
    
        if (g == "Java")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

输出:

The index of Java is: 2

笔记:

  • 从startIndex开始到第一个元素结束向后搜索List
  • Predicate 是方法的委托,如果传递给它的对象与委托中定义的条件匹配,则该方法返回true。当前List 的元素分别传递给Predicate 委托。
  • 该方法执行线性搜索;因此,此方法是O(n)运算,其中n是从List 的开头到开头的元素数。