List FindLastIndex() Method in C# | Set -2
Last Updated :
05 Dec, 2019
This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method:
Here, we will discuss only the last two methods.
FindLastIndex(Int32, Predicate<T>) Method
This method searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the last occurrence within the range of elements in the List<T> that extends from the first element to the specified index.
Syntax: public int FindLastIndex (int start, Predicate<T> match);
Parameters:
start: It is the starting index from the searching will starts.
match: It is the Predicate delegate that defines the conditions of the searched element.
Return Value: If the element is found then it returns the zero-based index of type Int32 of the last occurrence of an element that matches a specified condition by the parameter “match”. And if not found then it returns “-1”.
Exceptions:
- ArgumentNullException: If the match is null.
- ArgumentOutOfRangeException: If the startis outside the range of valid indexes for the List<T>.
Below programs illustrate the use of above-discussed method:
Example 1:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
List< string > PC = new List< string >();
PC.Add( "Computer" );
PC.Add( "keyboard" );
PC.Add( "laptop" );
PC.Add( "mouse" );
int indx = PC.FindLastIndex(2, FindIndex);
Console.WriteLine(indx);
}
private static bool FindIndex( string g)
{
if (g == "Computer" )
{
return true ;
}
else
{
return false ;
}
}
}
|
Output:
0
Example 2:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
List< int > PC = new List< int >();
PC.Add(3);
PC.Add(4);
PC.Add(5);
PC.Add(6);
int indx = PC.FindLastIndex(2, FindIndex);
Console.WriteLine(indx);
}
private static bool FindIndex( int g)
{
if (g == 5)
{
return true ;
}
else
{
return false ;
}
}
}
|
Output:
2
Example 3: In this example we use an XML file and search an item from a starting index and prints the index of that item, if the item is not found then prints “-1” and if found then prints the index. The item is “GeeksForGeeks”. But here we don’t have the XML file, and here the compiler gives an exception.
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
private static List<gfg> geeks = new List<gfg>();
public static void Main()
{
int x = geeks.FindLastIndex(3, FindGFG);
Console.WriteLine(x);
}
private static bool FindGFG(gfg g)
{
if (g.G == "GeeksForGeeks" )
{
return true ;
}
else
{
return false ;
}
}
}
public class gfg {
public string G
{
get ;
set ;
}
}
|
Runtime Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: ArgumentOutOfRange_Index
Parameter name: startIndex
FindLastIndex(Int32, Int32, Predicate<T>) Method
This method searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the last occurrence within the entire List and the list contains the specified number of elements and ends at the specified index.
Syntax: public int FindLastIndex (int startIndex, int count, Predicate<T> match);
Parameters:
startIndex: It is the zero-based starting index of the backward search.
count: It is the number of elements in the section to search.
match: It is the Predicate<T> delegate that defines the conditions of the element to search for.
Return Value: If the element is found then it returns the zero-based index of type Int32 of the last element that matches a specified condition by the parameter “match”. And if not found then it returns “-1”.
Exceptions:
- ArgumentNullException: if the “match” is null.
- ArgumentOutOfRangeException: if “startIndex” is outside the range or “count” is less than 0(Zero) or “startIndex” and “count” do not specify a valid section in the List
Example:
using System;
using System.Collections.Generic;
class GFG
{
public static void Main()
{
List< string > mylist = new List< string >();
mylist.Add( "C" );
mylist.Add( "C++" );
mylist.Add( "Java" );
mylist.Add( "Python" );
mylist.Add( "C#" );
mylist.Add( "HTML" );
mylist.Add( "Java" );
mylist.Add( "PHP" );
int indx = mylist.FindLastIndex(2, 3, FindIndex);
Console.WriteLine( "The index of Java is: " +indx);
}
private static bool FindIndex( string g)
{
if (g == "Java" )
{
return true ;
}
else
{
return false ;
}
}
}
|
Output:
The index of Java is: 2
Note:
- The List<T> is searched backward starting at startIndex and ending at the first element.
- The Predicate<T> is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current List<T> are individually passed to the Predicate<T> delegate.
- This method performs a linear search; therefore, this method is an O(n) operation, where n is the number of elements from the beginning of the List<T> to start.
Share your thoughts in the comments
Please Login to comment...