Array.FindIndex 메서드

정의

지정한 조건자에 정의된 조건과 일치하는 요소를 검색하여 Array 또는 그 일부에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

오버로드

FindIndex<T>(T[], Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하여 전체 Array에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

FindIndex<T>(T[], Int32, Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하여 지정된 인덱스에서 마지막 요소로 확장하는 Array의 요소 범위에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

FindIndex<T>(T[], Int32, Int32, Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하고 지정된 인덱스부터 시작하여 지정된 수의 요소를 포함하는 Array의 요소 범위에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

예제

다음 코드 예제에서는 제네릭 메서드의 세 가지 오버로드를 FindIndex 모두 보여 줍니다. 8개의 공룡 이름을 포함하는 문자열 배열이 생성되며, 그 중 2개(위치 1과 5)는 "사우루스"로 끝납니다. 또한 코드 예제는 문자열 매개 변수를 수락하고 입력 문자열이 "saurus"로 끝나는지 여부를 나타내는 부울 값을 반환하는 검색 EndsWithSaurus조건자 메서드를 정의합니다.

FindIndex<T>(T[], Predicate<T>) 메서드 오버로드는 처음부터 배열을 트래버스하여 각 요소를 차례로 메서드에 EndsWithSaurus 전달합니다. 메서드가 위치 1의 EndsWithSaurus 요소에 대해 반환 true 되면 검색이 중지됩니다.

참고

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을 반환합니다.

using namespace System;

// Search predicate returns true if a string ends in "saurus".
bool EndsWithSaurus(String^ s)
{
    if ((s->Length > 5) && 
        (s->Substring(s->Length - 6)->ToLower() == "saurus"))
    {
        return true;
    }
    else
    {
        return false;
    }
};

void main()
{
    array<String^>^ dinosaurs = { "Compsognathus", 
        "Amargasaurus",   "Oviraptor",      "Velociraptor", 
        "Deinonychus",    "Dilophosaurus",  "Gallimimus", 
        "Triceratops" };

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nArray::FindIndex(dinosaurs, EndsWithSaurus): {0}", 
        Array::FindIndex(dinosaurs, gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nArray::FindIndex(dinosaurs, 2, EndsWithSaurus): {0}",
        Array::FindIndex(dinosaurs, 2, gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nArray::FindIndex(dinosaurs, 2, 3, EndsWithSaurus): {0}",
        Array::FindIndex(dinosaurs, 2, 3, gcnew Predicate<String^>(EndsWithSaurus)));
}

/* 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
 */
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에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

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[]

검색할 1차원 Array(인덱스는 0부터 시작)입니다.

match
Predicate<T>

검색할 요소의 조건을 정의하는 Predicate<T>입니다.

반환

Int32

match에 정의된 조건과 일치하는 요소가 있으면 일치하는 요소 중 첫 번째로 나타나는 요소의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.

예외

array이(가) null인 경우

또는

match이(가) null인 경우

설명

Array 번째 요소에서 시작하여 마지막 요소에서 끝나는 앞으로 검색됩니다.

전달 Predicate<T> 된 개체가 대리자에서 정의된 조건과 일치하는지 반환 true 하는 메서드에 대한 대리자입니다. 의 array 요소는 개별적으로 .에 Predicate<T>전달됩니다.

이 메서드는 O(n) 연산입니다. 여기서 nLength array.

추가 정보

적용 대상

FindIndex<T>(T[], Int32, Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하여 지정된 인덱스에서 마지막 요소로 확장하는 Array의 요소 범위에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

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[]

검색할 1차원 Array(인덱스는 0부터 시작)입니다.

startIndex
Int32

검색의 0부터 시작하는 인덱스입니다.

match
Predicate<T>

검색할 요소의 조건을 정의하는 Predicate<T>입니다.

반환

Int32

match에 정의된 조건과 일치하는 요소가 있으면 일치하는 요소 중 첫 번째로 나타나는 요소의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.

예외

array이(가) null인 경우.

또는

match이(가) null인 경우

startIndexarray의 유효한 인덱스 범위를 벗어납니다.

설명

마지막 Array 요소에서 startIndex 시작하여 끝나는 앞으로 검색됩니다.

전달 Predicate<T> 된 개체가 대리자에서 정의된 조건과 일치하는지 반환 true 하는 메서드에 대한 대리자입니다. 의 array 요소는 개별적으로 .에 Predicate<T>전달됩니다.

이 메서드는 O(n) 연산으로, 여기서 n 는 요소의 수부터 끝까지의 array요소 startIndex 수입니다.

추가 정보

적용 대상

FindIndex<T>(T[], Int32, Int32, Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하고 지정된 인덱스부터 시작하여 지정된 수의 요소를 포함하는 Array의 요소 범위에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

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[]

검색할 1차원 Array(인덱스는 0부터 시작)입니다.

startIndex
Int32

검색의 0부터 시작하는 인덱스입니다.

count
Int32

검색할 섹션에 있는 요소 수입니다.

match
Predicate<T>

검색할 요소의 조건을 정의하는 Predicate<T>입니다.

반환

Int32

match에 정의된 조건과 일치하는 요소가 있으면 일치하는 요소 중 첫 번째로 나타나는 요소의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.

예외

array이(가) null인 경우

또는

match이(가) null인 경우

startIndexarray의 유효한 인덱스 범위를 벗어납니다.

또는

count가 0보다 작은 경우

또는

startIndexcountarray의 올바른 섹션을 지정하지 않습니다.

설명

Array 0보다 큰 경우 count 더하기 count 1에서 startIndex startIndex 시작하여 끝나는 앞으로 검색됩니다.

전달 Predicate<T> 된 개체가 대리자에서 정의된 조건과 일치하는지 반환 true 하는 메서드에 대한 대리자입니다. 의 array 요소는 개별적으로 .에 Predicate<T>전달됩니다.

이 메서드는 O (n) 작업, 여기서 ncount합니다.

추가 정보

적용 대상