共用方式為


Array.findIndex<'T> 函式 (F#)

傳回陣列中滿足所指定述詞之第一個元素的索引。 引發 KeyNotFoundException如果無項目滿足 述詞。

命名空間/模組路徑:Microsoft.FSharp.Collections.Array

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
Array.findIndex : ('T -> bool) -> 'T [] -> int

// Usage:
Array.findIndex predicate array

參數

  • predicate
    型別:'T -> bool

    用來測試輸入項目的函式。

  • array
    型別:'T []

    輸入陣列。

例外狀況

例外狀況

條件

KeyNotFoundException

如果擲回 predicate不會傳回 true的任何項目。

傳回值

陣列中滿足所指定述詞之第一個項目的索引。

備註

這個函式是名為 FindIndex中 已編譯的組件。 如果從一個語言,F # 以外,或透過反映存取函式使用這個名稱。

範例

下列範例示範使用 Array.findArray.findIndex來識別第一個整數,則大於 1 的是方形和 Cube。

let arrayA = [| 2 .. 100 |]
let delta = 1.0e-10
let isPerfectSquare (x:int) =
    let y = sqrt (float x)
    abs(y - round y) < delta
let isPerfectCube (x:int) =
    let y = System.Math.Pow(float x, 1.0/3.0)
    abs(y - round y) < delta
let element = Array.find (fun elem -> isPerfectSquare elem && isPerfectCube elem) arrayA
let index = Array.findIndex (fun elem -> isPerfectSquare elem && isPerfectCube elem) arrayA
printfn "The first element that is both a square and a cube is %d and its index is %d." element index
  

平台

Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2

版本資訊

F# 執行階段

支援版本:2.0、4.0

Silverlight

支援版本:3

請參閱

參考

Collections.Array 模組 (F#)

Microsoft.FSharp.Collections 命名空間 (F#)

Array.find<'T> 函式 (F#)