Array.FindLastIndex 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
搜索与指定谓词定义的条件匹配的元素,并返回其某个或部分内 Array 最后一个匹配项的从零开始的索引。
重载
| 名称 | 说明 |
|---|---|
| FindLastIndex<T>(T[], Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并返回整个 Array中最后一个匹配项的从零开始的索引。 |
| FindLastIndex<T>(T[], Int32, Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并在从第一个元素扩展到指定索引的元素 Array 范围内返回最后一个匹配项的从零开始的索引。 |
| FindLastIndex<T>(T[], Int32, Int32, Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并在包含指定索引的元素 Array 范围内返回最后一个匹配项的从零开始的索引。 |
示例
下面的代码示例演示泛型方法的所有三个 FindLastIndex 重载。 将创建一个字符串数组,其中包含 8 个恐龙名称,其中两个名称(在位置 1 和 5)以“saurus”结尾。 该代码示例还定义了一个名为 EndsWithSaurus的搜索谓词方法,该方法接受字符串参数并返回一个布尔值,该值指示输入字符串是否以“saurus”结尾。
方法 FindLastIndex<T>(T[], Predicate<T>) 重载从末尾向后遍历数组,并依次将每个元素传递给 EndsWithSaurus 该方法。 当方法返回EndsWithSaurus位置 5 处的元素时true,搜索将停止。
注释
在 C#、F# 和 Visual Basic 中,不需要显式创建 Predicate<string> 委托(Visual Basic Predicate(Of String))。 这些语言从上下文推断出正确的委托,并自动创建它。
方法 FindLastIndex<T>(T[], Int32, Predicate<T>) 重载用于搜索从位置 4 开始的数组,并继续向后到数组的开头。 它查找位置 1 处的元素。 最后,该方法 FindLastIndex<T>(T[], Int32, Int32, Predicate<T>) 重载用于搜索从位置 4 开始的三个元素的范围,然后向后工作(即元素 4、3 和 2)。 它返回 -1,因为该范围中没有以“saurus”结尾的恐龙名称。
using System;
public class Example
{
public static void Main()
{
string[] dinosaurs = { "Compsognathus",
"Amargasaurus", "Oviraptor", "Velociraptor",
"Deinonychus", "Dilophosaurus", "Gallimimus",
"Triceratops" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.FindLastIndex(dinosaurs, EndsWithSaurus): {0}",
Array.FindLastIndex(dinosaurs, EndsWithSaurus));
Console.WriteLine(
"\nArray.FindLastIndex(dinosaurs, 4, EndsWithSaurus): {0}",
Array.FindLastIndex(dinosaurs, 4, EndsWithSaurus));
Console.WriteLine(
"\nArray.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): {0}",
Array.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus));
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
}
/* This code example produces the following output:
Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops
Array.FindLastIndex(dinosaurs, EndsWithSaurus): 5
Array.FindLastIndex(dinosaurs, 4, EndsWithSaurus): 1
Array.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): -1
*/
open System
// Search predicate returns true if a string ends in "saurus".
let endsWithSaurus (s: string) =
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "saurus"
let dinosaurs =
[| "Compsognathus"; "Amargasaurus"
"Oviraptor"; "Velociraptor"
"Deinonychus"; "Dilophosaurus"
"Gallimimus"; "Triceratops" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.FindLastIndex(dinosaurs, endsWithSaurus)
|> printfn "\nArray.FindLastIndex(dinosaurs, EndsWithSaurus): %i"
Array.FindLastIndex(dinosaurs, 4, endsWithSaurus)
|> printfn "\nArray.FindLastIndex(dinosaurs, 4, EndsWithSaurus): %i"
Array.FindLastIndex(dinosaurs, 4, 3, endsWithSaurus)
|> printfn "\nArray.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): %i"
// This code example produces the following output:
//
// Compsognathus
// Amargasaurus
// Oviraptor
// Velociraptor
// Deinonychus
// Dilophosaurus
// Gallimimus
// Triceratops
//
// Array.FindLastIndex(dinosaurs, EndsWithSaurus): 5
//
// Array.FindLastIndex(dinosaurs, 4, EndsWithSaurus): 1
//
// Array.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Compsognathus", _
"Amargasaurus", "Oviraptor", "Velociraptor", _
"Deinonychus", "Dilophosaurus", "Gallimimus", _
"Triceratops" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.FindLastIndex(dinosaurs, AddressOf EndsWithSaurus): {0}", _
Array.FindLastIndex(dinosaurs, AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"Array.FindLastIndex(dinosaurs, 4, AddressOf EndsWithSaurus): {0}", _
Array.FindLastIndex(dinosaurs, 4, AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"Array.FindLastIndex(dinosaurs, 4, 3, AddressOf EndsWithSaurus): {0}", _
Array.FindLastIndex(dinosaurs, 4, 3, AddressOf EndsWithSaurus))
End Sub
' Search predicate returns true if a string ends in "saurus".
Private Shared Function EndsWithSaurus(ByVal s As String) _
As Boolean
' AndAlso prevents evaluation of the second Boolean
' expression if the string is so short that an error
' would occur.
If (s.Length > 5) AndAlso _
(s.Substring(s.Length - 6).ToLower() = "saurus") Then
Return True
Else
Return False
End If
End Function
End Class
' This code example produces the following output:
'
'Compsognathus
'Amargasaurus
'Oviraptor
'Velociraptor
'Deinonychus
'Dilophosaurus
'Gallimimus
'Triceratops
'
'Array.FindLastIndex(dinosaurs, AddressOf EndsWithSaurus): 5
'
'Array.FindLastIndex(dinosaurs, 4, AddressOf EndsWithSaurus): 1
'
'Array.FindLastIndex(dinosaurs, 4, 3, AddressOf EndsWithSaurus): -1
FindLastIndex<T>(T[], Predicate<T>)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索与指定谓词定义的条件匹配的元素,并返回整个 Array中最后一个匹配项的从零开始的索引。
public:
generic <typename T>
static int FindLastIndex(cli::array <T> ^ array, Predicate<T> ^ match);
public static int FindLastIndex<T>(T[] array, Predicate<T> match);
static member FindLastIndex : 'T[] * Predicate<'T> -> int
Public Shared Function FindLastIndex(Of T) (array As T(), match As Predicate(Of T)) As Integer
类型参数
- T
数组元素的类型。
参数
- array
- T[]
从零开始 Array 的一维搜索。
- match
- Predicate<T>
定义 Predicate<T> 要搜索的元素的条件。
返回
如果找到,则为与所定义的 match条件匹配的元素的最后一个匹配项的从零开始的索引;否则为 -1。
例外
注解
从 Array 最后一个元素开始搜索向后搜索,最后一个元素结束。
Predicate<T>是方法的委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回true该方法。 的元素array单独传递给 .Predicate<T>
此方法是 O(n) 操作,其位置 n 为 Lengtharray.
另请参阅
- Exists<T>(T[], Predicate<T>)
- Find<T>(T[], Predicate<T>)
- FindLast<T>(T[], Predicate<T>)
- FindAll<T>(T[], Predicate<T>)
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>
适用于
FindLastIndex<T>(T[], Int32, Predicate<T>)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索与指定谓词定义的条件匹配的元素,并在从第一个元素扩展到指定索引的元素 Array 范围内返回最后一个匹配项的从零开始的索引。
public:
generic <typename T>
static int FindLastIndex(cli::array <T> ^ array, int startIndex, Predicate<T> ^ match);
public static int FindLastIndex<T>(T[] array, int startIndex, Predicate<T> match);
static member FindLastIndex : 'T[] * int * Predicate<'T> -> int
Public Shared Function FindLastIndex(Of T) (array As T(), startIndex As Integer, match As Predicate(Of T)) As Integer
类型参数
- T
数组元素的类型。
参数
- array
- T[]
从零开始 Array 的一维搜索。
- startIndex
- Int32
从零开始的向后搜索索引。
- match
- Predicate<T>
定义 Predicate<T> 要搜索的元素的条件。
返回
如果找到,则为与所定义的 match条件匹配的元素的最后一个匹配项的从零开始的索引;否则为 -1。
例外
startIndex 超出了有效索引 array的范围。
注解
从 Array 第一个元素开始 startIndex 和结束搜索后移。
Predicate<T>是方法的委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回true该方法。 的元素array单独传递给 .Predicate<T>
此方法是一个 O(n) 操作,其中 n 元素数从开头 array 到 startIndex。
另请参阅
- Exists<T>(T[], Predicate<T>)
- Find<T>(T[], Predicate<T>)
- FindLast<T>(T[], Predicate<T>)
- FindAll<T>(T[], Predicate<T>)
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>
适用于
FindLastIndex<T>(T[], Int32, Int32, Predicate<T>)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索与指定谓词定义的条件匹配的元素,并在包含指定索引的元素 Array 范围内返回最后一个匹配项的从零开始的索引。
public:
generic <typename T>
static int FindLastIndex(cli::array <T> ^ array, int startIndex, int count, Predicate<T> ^ match);
public static int FindLastIndex<T>(T[] array, int startIndex, int count, Predicate<T> match);
static member FindLastIndex : 'T[] * int * int * Predicate<'T> -> int
Public Shared Function FindLastIndex(Of T) (array As T(), startIndex As Integer, count As Integer, match As Predicate(Of T)) As Integer
类型参数
- T
数组元素的类型。
参数
- array
- T[]
从零开始 Array 的一维搜索。
- startIndex
- Int32
从零开始的向后搜索索引。
- count
- Int32
要搜索的节中的元素数。
- match
- Predicate<T>
定义 Predicate<T> 要搜索的元素的条件。
返回
如果找到,则为与所定义的 match条件匹配的元素的最后一个匹配项的从零开始的索引;否则为 -1。
例外
startIndex 超出了有效索引 array的范围。
-或-
count 小于零。
-或-
startIndex 且 count 未在 . 中 array指定有效节。
注解
如果Array大于 0,startIndex则从负startIndex加 1 开始搜索count后向count搜索。
Predicate<T>是方法的委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回true该方法。 的元素array单独传递给 .Predicate<T>
此方法是 O(n) 操作,其中 n 。count
另请参阅
- Exists<T>(T[], Predicate<T>)
- Find<T>(T[], Predicate<T>)
- FindLast<T>(T[], Predicate<T>)
- FindAll<T>(T[], Predicate<T>)
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>