Share via


Funzione Array.findIndex<'T> (F#)

Restituisce l'indice del primo elemento nella matrice che soddisfa il predicato specificato. Genera l'eccezione KeyNotFoundException se nessun elemento soddisfa il predicato.

Percorso di spazio dei nomi/modulo: Microsoft.FSharp.Collections.Array

Assembly: FSharp.Core (in FSharp.Core.dll)

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

// Usage:
Array.findIndex predicate array

Parametri

  • predicate
    Tipo: 'T -> bool

    Funzione utilizzata per eseguire il test degli elementi di input.

  • array
    Tipo: 'T []

    Matrice di input.

Eccezioni

Eccezione

Condizione

KeyNotFoundException

Viene generata se predicate non restituisce true per ogni elemento.

Valore restituito

Indice del primo elemento nella matrice che soddisfa il predicato specificato.

Note

Questa funzione è denominata FindIndex negli assembly compilati. Utilizzare questo nome se si accede alla funzione da un linguaggio diverso da F# o tramite reflection.

Esempio

Nell'esempio riportato di seguito viene illustrato l'utilizzo di Array.find e Array.findIndex per identificare il primo numero intero maggiore di 1 che è sia un quadrato che un cubo.

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
  

Piattaforme

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2.

Informazioni sulla versione

F# Runtime

Supportato in: 2.0, 4.0

Silverlight

Supportato in: 3

Vedere anche

Riferimenti

Modulo Collections.Array (F#)

Spazio dei nomi Microsoft.FSharp.Collections (F#)

Funzione Array.find<'T> (F#)