Array.FindLastIndex 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
搜尋符合指定謂詞定義條件的元素,並回傳該謂詞中最後一次出現 Array 的零為基礎索引。
多載
| 名稱 | Description |
|---|---|
| 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 方法返回 true 第 5 位元素時,搜尋即停止。
Note
在 C#、F# 和 Visual Basic 中,無需明確建立 Predicate<string>代理(Predicate(Of String) 在 Visual Basic 中)。 這些語言會從上下文推斷正確的代理並自動建立。
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>)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- 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 為 Length 的 array。
另請參閱
- 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>)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- 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>)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- 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當 startIndex 大於 0 時,則從 開始startIndex至結束count於負count加 1 的逆向搜尋。
是 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>