Array.FindIndex 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
搜尋符合指定謂詞定義條件的元素,並回傳該元素中首次出現的零為基礎的索引,該指標位於該條件的一 Array 部分或部分內。
多載
| 名稱 | Description |
|---|---|
| FindIndex<T>(T[], Predicate<T>) |
搜尋符合指定謂詞定義條件的元素,並回傳整個 Array中首次出現的零基索引。 |
| FindIndex<T>(T[], Int32, Predicate<T>) |
搜尋符合指定謂詞定義條件的元素,並回傳從指定索引延伸到最後一個元素範圍內,首次出現 Array 的零為基礎的索引。 |
| FindIndex<T>(T[], Int32, Int32, Predicate<T>) |
搜尋符合指定謂詞定義條件的元素,並回傳從指定索引開始且包含指定數量元素的元素範圍內首次出現 Array 的零為基礎的索引。 |
範例
以下程式碼範例展示了通用方法的三種超載 FindIndex 方式。 會建立一個包含8個恐龍名字的字串陣列,其中兩個(在第1和第5位置)以「saurus」結尾。 程式碼範例也定義了一個名為 EndsWithSaurus的搜尋謂詞方法,該方法接受字串參數並回傳一個布林值,指示輸入字串是否以「saurus」結尾。
FindIndex<T>(T[], Predicate<T>)方法過載會從一開始遍歷陣列,依序將每個元素傳遞給方法。EndsWithSaurus 當 EndsWithSaurus 方法返回 true 第 1 位元素時,搜尋即停止。
Note
在 C#、F# 和 Visual Basic 中,無需明確建立 Predicate<string>代理(Predicate(Of String) 在 Visual Basic)。 這些語言會從上下文推斷正確的代理並自動建立。
FindIndex<T>(T[], Int32, Predicate<T>)方法過載用於從位置 2 開始搜尋陣列,一直搜尋到陣列末端。 它會在第 5 位找到元素。 最後, FindIndex<T>(T[], Int32, Int32, Predicate<T>) 方法過載用於搜尋從位置 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.FindIndex(dinosaurs, EndsWithSaurus): {0}",
Array.FindIndex(dinosaurs, EndsWithSaurus));
Console.WriteLine(
"\nArray.FindIndex(dinosaurs, 2, EndsWithSaurus): {0}",
Array.FindIndex(dinosaurs, 2, EndsWithSaurus));
Console.WriteLine(
"\nArray.FindIndex(dinosaurs, 2, 3, EndsWithSaurus): {0}",
Array.FindIndex(dinosaurs, 2, 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.FindIndex(dinosaurs, EndsWithSaurus): 1
Array.FindIndex(dinosaurs, 2, EndsWithSaurus): 5
Array.FindIndex(dinosaurs, 2, 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.FindIndex(dinosaurs, endsWithSaurus)
|> printfn "\nArray.FindIndex(dinosaurs, EndsWithSaurus): %i"
Array.FindIndex(dinosaurs, 2, endsWithSaurus)
|> printfn "\nArray.FindIndex(dinosaurs, 2, EndsWithSaurus): %i"
Array.FindIndex(dinosaurs, 2, 3, endsWithSaurus)
|> printfn "\nArray.FindIndex(dinosaurs, 2, 3, EndsWithSaurus): %i"
// This code example produces the following output:
//
// Compsognathus
// Amargasaurus
// Oviraptor
// Velociraptor
// Deinonychus
// Dilophosaurus
// Gallimimus
// Triceratops
//
// Array.FindIndex(dinosaurs, EndsWithSaurus): 1
//
// Array.FindIndex(dinosaurs, 2, EndsWithSaurus): 5
//
// Array.FindIndex(dinosaurs, 2, 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.FindIndex(dinosaurs, AddressOf EndsWithSaurus): {0}", _
Array.FindIndex(dinosaurs, AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"Array.FindIndex(dinosaurs, 2, AddressOf EndsWithSaurus): {0}", _
Array.FindIndex(dinosaurs, 2, AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"Array.FindIndex(dinosaurs, 2, 3, AddressOf EndsWithSaurus): {0}", _
Array.FindIndex(dinosaurs, 2, 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.FindIndex(dinosaurs, AddressOf EndsWithSaurus): 1
'
'Array.FindIndex(dinosaurs, 2, AddressOf EndsWithSaurus): 5
'
'Array.FindIndex(dinosaurs, 2, 3, AddressOf EndsWithSaurus): -1
FindIndex<T>(T[], Predicate<T>)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
搜尋符合指定謂詞定義條件的元素,並回傳整個 Array中首次出現的零基索引。
public:
generic <typename T>
static int FindIndex(cli::array <T> ^ array, Predicate<T> ^ match);
public static int FindIndex<T>(T[] array, Predicate<T> match);
static member FindIndex : 'T[] * Predicate<'T> -> int
Public Shared Function FindIndex(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>
適用於
FindIndex<T>(T[], Int32, Predicate<T>)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
搜尋符合指定謂詞定義條件的元素,並回傳從指定索引延伸到最後一個元素範圍內,首次出現 Array 的零為基礎的索引。
public:
generic <typename T>
static int FindIndex(cli::array <T> ^ array, int startIndex, Predicate<T> ^ match);
public static int FindIndex<T>(T[] array, int startIndex, Predicate<T> match);
static member FindIndex : 'T[] * int * Predicate<'T> -> int
Public Shared Function FindIndex(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 是從 startIndex 到 末 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>
適用於
FindIndex<T>(T[], Int32, Int32, Predicate<T>)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
搜尋符合指定謂詞定義條件的元素,並回傳從指定索引開始且包含指定數量元素的元素範圍內首次出現 Array 的零為基礎的索引。
public:
generic <typename T>
static int FindIndex(cli::array <T> ^ array, int startIndex, int count, Predicate<T> ^ match);
public static int FindIndex<T>(T[] array, int startIndex, int count, Predicate<T> match);
static member FindIndex : 'T[] * int * int * Predicate<'T> -> int
Public Shared Function FindIndex(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向前搜尋,直到正countcount負 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>