Array.LastIndexOf 메서드

정의

1차원 Array 또는 Array 일부에서 지정한 값과 마지막으로 일치하는 요소의 인덱스를 반환합니다.

오버로드

LastIndexOf(Array, Object)

지정한 개체를 검색하여 전체 1차원 Array내에서 마지막으로 검색된 값의 인덱스를 반환합니다.

LastIndexOf(Array, Object, Int32)

지정한 개체를 검색하여 첫 번째 요소에서 지정한 인덱스로 확장하는 1차원 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

LastIndexOf(Array, Object, Int32, Int32)

지정한 개체를 검색하여 지정한 수의 요소를 포함하고 지정한 인덱스에서 끝나는 1차원 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

LastIndexOf<T>(T[], T)

지정한 개체를 검색하여 전체 Array에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

LastIndexOf<T>(T[], T, Int32)

지정한 개체를 검색하여 첫 번째 요소에서 지정한 인덱스로 확장하는 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

LastIndexOf<T>(T[], T, Int32, Int32)

지정한 개체를 검색하여 지정한 수의 요소를 포함하고 지정한 인덱스에서 끝나는 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

LastIndexOf(Array, Object)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

지정한 개체를 검색하여 전체 1차원 Array내에서 마지막으로 검색된 값의 인덱스를 반환합니다.

public:
 static int LastIndexOf(Array ^ array, System::Object ^ value);
public static int LastIndexOf (Array array, object value);
public static int LastIndexOf (Array array, object? value);
static member LastIndexOf : Array * obj -> int
Public Shared Function LastIndexOf (array As Array, value As Object) As Integer

매개 변수

array
Array

검색할 1차원 Array 입니다.

value
Object

array에서 찾을 개체입니다.

반환

검색된 value가 있으면 array 전체에서 마지막으로 검색된 값의 인덱스이고, 그렇지 않으면 배열의 하한에서 1을 뺀 값입니다.

예외

array이(가) null인 경우

array가 다차원 배열인 경우

예제

다음 코드 예제에서는 배열에서 지정된 요소의 마지막 발생 인덱스 확인 하는 방법을 보여 주는 합니다.

using namespace System;
void PrintIndexAndValues( Array^ myArray );

void main()
{
   // Creates and initializes a new Array instance with three elements of the same value.
   Array^ myArray = Array::CreateInstance( String::typeid, 12 );
   myArray->SetValue( "the", 0 );
   myArray->SetValue( "quick", 1 );
   myArray->SetValue( "brown", 2 );
   myArray->SetValue( "fox", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );
   myArray->SetValue( "in", 9 );
   myArray->SetValue( "the", 10 );
   myArray->SetValue( "barn", 11 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance contains the following values:" );
   PrintIndexAndValues( myArray );

   // Searches for the last occurrence of the duplicated value.
   String^ myString =  "the";
   int myIndex = Array::LastIndexOf( myArray, myString );
   Console::WriteLine(  "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in the first section of the Array.
   myIndex = Array::LastIndexOf( myArray, myString, 8 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in a section of the Array.  
   // Note that the start index is greater than the end index because the search is done backward.
   myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */
let printIndexAndValues (arr: 'a []) =
   for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
      printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array with three elements of the same value.
let myArray = 
   [| "the"; "quick"; "brown"; "fox"
      "jumps"; "over"; "the"; "lazy"
      "dog"; "in"; "the"; "barn" |]

// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray

// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."


//      This code produces the following output.
//
//      The Array contains the following values:
//         [0]:    the
//         [1]:    quick
//         [2]:    brown
//         [3]:    fox
//         [4]:    jumps
//         [5]:    over
//         [6]:    the
//         [7]:    lazy
//         [8]:    dog
//         [9]:    in
//         [10]:    the
//         [11]:    barn
//      The last occurrence of "the" is at index 10.
//      The last occurrence of "the" between the start and index 8 is at index 6.
//      The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces the following output.

The Array contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array with three elements of
        ' the same value.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
        myArray.SetValue("the", 0)
        myArray.SetValue("quick", 1)
        myArray.SetValue("brown", 2)
        myArray.SetValue("fox", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        myArray.SetValue("in", 9)
        myArray.SetValue("the", 10)
        myArray.SetValue("barn", 11)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintIndexAndValues(myArray)
        
        ' Searches for the last occurrence of the duplicated value.
        Dim myString As String = "the"
        Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
        Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
           myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in the first
        ' section of the Array.
        myIndex = Array.LastIndexOf(myArray, myString, 8)
        Console.WriteLine("The last occurrence of ""{0}"" between the start " _
           + "and index 8 is at index {1}.", myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in a section
        ' of the Array.  Note that the start index is greater than the end
        ' index because the search is done backward.
        myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
        Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
           + "and index 10 is at index {1}.", myString, myIndex)
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The Array contains the following values:
'     [0]:    the
'     [1]:    quick
'     [2]:    brown
'     [3]:    fox
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
'     [9]:    in
'     [10]:    the
'     [11]:    barn
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.

설명

1차원 Array 은 마지막 요소에서 시작하여 첫 번째 요소에서 끝나는 뒤로 검색됩니다.

요소는 메서드를 사용하여 지정된 값과 Object.Equals 비교됩니다. 요소 형식이 기본이 아닌(사용자 정의) 형식 Equals 인 경우 해당 형식의 구현이 사용됩니다.

대부분의 배열은 하한이 0이므로 이 메서드는 가 없으면 value 일반적으로 -1을 반환합니다. 드물게 배열의 하한이 같 Int32.MinValuevalue 찾을 수 없는 경우 이 메서드는 인 를 System.Int32.MinValue - 1반환합니다Int32.MaxValue.

이 메서드는 O(n) 작업입니다. 여기서 n 는 의 array입니다Length.

.NET Framework 2.0 이상 버전에서 이 메서드는 의 ArrayCompareTo 메서드를 사용하여 Equals 매개 변수에 value 지정된 가 Object 있는지 여부를 확인합니다. 이전 버전의 .NET Framework 자체의 valueObjectCompareTo 메서드를 Equals 사용하여 이 결정을 내렸습니다.

CompareTo 메서드는 컬렉션의 item 개체에 대한 매개 변수입니다.

추가 정보

적용 대상

LastIndexOf(Array, Object, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

지정한 개체를 검색하여 첫 번째 요소에서 지정한 인덱스로 확장하는 1차원 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

public:
 static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int LastIndexOf (Array array, object value, int startIndex);
public static int LastIndexOf (Array array, object? value, int startIndex);
static member LastIndexOf : Array * obj * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer) As Integer

매개 변수

array
Array

검색할 1차원 Array 입니다.

value
Object

array에서 찾을 개체입니다.

startIndex
Int32

뒤로 검색할 시작 인덱스입니다.

반환

value가 있을 경우 첫 번째 요소에서 array로 확장하는 startIndex의 요소 범위에서 마지막으로 검색된 값의 인덱스이고, 그렇지 않으면 배열의 하한에서 1을 뺀 값입니다.

예외

array이(가) null인 경우

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

array가 다차원 배열인 경우

예제

다음 코드 예제에서는 배열에서 지정된 요소의 마지막 발생 인덱스 확인 하는 방법을 보여 주는 합니다.

using namespace System;
void PrintIndexAndValues( Array^ myArray );

void main()
{
   // Creates and initializes a new Array instance with three elements of the same value.
   Array^ myArray = Array::CreateInstance( String::typeid, 12 );
   myArray->SetValue( "the", 0 );
   myArray->SetValue( "quick", 1 );
   myArray->SetValue( "brown", 2 );
   myArray->SetValue( "fox", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );
   myArray->SetValue( "in", 9 );
   myArray->SetValue( "the", 10 );
   myArray->SetValue( "barn", 11 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance contains the following values:" );
   PrintIndexAndValues( myArray );

   // Searches for the last occurrence of the duplicated value.
   String^ myString =  "the";
   int myIndex = Array::LastIndexOf( myArray, myString );
   Console::WriteLine(  "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in the first section of the Array.
   myIndex = Array::LastIndexOf( myArray, myString, 8 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in a section of the Array.  
   // Note that the start index is greater than the end index because the search is done backward.
   myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */
let printIndexAndValues (arr: 'a []) =
   for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
      printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array with three elements of the same value.
let myArray = 
   [| "the"; "quick"; "brown"; "fox"
      "jumps"; "over"; "the"; "lazy"
      "dog"; "in"; "the"; "barn" |]

// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray

// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."


//      This code produces the following output.
//
//      The Array contains the following values:
//         [0]:    the
//         [1]:    quick
//         [2]:    brown
//         [3]:    fox
//         [4]:    jumps
//         [5]:    over
//         [6]:    the
//         [7]:    lazy
//         [8]:    dog
//         [9]:    in
//         [10]:    the
//         [11]:    barn
//      The last occurrence of "the" is at index 10.
//      The last occurrence of "the" between the start and index 8 is at index 6.
//      The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces the following output.

The Array contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array with three elements of
        ' the same value.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
        myArray.SetValue("the", 0)
        myArray.SetValue("quick", 1)
        myArray.SetValue("brown", 2)
        myArray.SetValue("fox", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        myArray.SetValue("in", 9)
        myArray.SetValue("the", 10)
        myArray.SetValue("barn", 11)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintIndexAndValues(myArray)
        
        ' Searches for the last occurrence of the duplicated value.
        Dim myString As String = "the"
        Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
        Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
           myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in the first
        ' section of the Array.
        myIndex = Array.LastIndexOf(myArray, myString, 8)
        Console.WriteLine("The last occurrence of ""{0}"" between the start " _
           + "and index 8 is at index {1}.", myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in a section
        ' of the Array.  Note that the start index is greater than the end
        ' index because the search is done backward.
        myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
        Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
           + "and index 10 is at index {1}.", myString, myIndex)
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The Array contains the following values:
'     [0]:    the
'     [1]:    quick
'     [2]:    brown
'     [3]:    fox
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
'     [9]:    in
'     [10]:    the
'     [11]:    barn
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.

설명

1차원 Array 은 에서 시작하여 첫 번째 요소에서 startIndex 끝나는 뒤로 검색됩니다.

요소는 메서드를 사용하여 지정된 값과 Object.Equals 비교됩니다. 요소 형식이 기본이 아닌(사용자 정의) 형식 Equals 인 경우 해당 형식의 구현이 사용됩니다.

대부분의 배열은 하한이 0이므로 이 메서드는 가 없으면 value 일반적으로 -1을 반환합니다. 드물게 배열의 하한이 같 Int32.MinValuevalue 찾을 수 없는 경우 이 메서드는 인 를 System.Int32.MinValue - 1반환합니다Int32.MaxValue.

이 메서드는 O(n) 연산입니다. 여기서 n 는 의 arraystartIndex시작 부분에서 부터 로의 요소 수입니다.

.NET Framework 2.0 이상 버전에서 이 메서드는 의 ArrayCompareTo 메서드를 사용하여 Equals 매개 변수에 value 지정된 가 Object 있는지 여부를 확인합니다. 이전 버전의 .NET Framework 자체의 valueObjectCompareTo 메서드를 Equals 사용하여 이 결정을 내렸습니다.

추가 정보

적용 대상

LastIndexOf(Array, Object, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

지정한 개체를 검색하여 지정한 수의 요소를 포함하고 지정한 인덱스에서 끝나는 1차원 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

public:
 static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int LastIndexOf (Array array, object value, int startIndex, int count);
public static int LastIndexOf (Array array, object? value, int startIndex, int count);
static member LastIndexOf : Array * obj * int * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer

매개 변수

array
Array

검색할 1차원 Array 입니다.

value
Object

array에서 찾을 개체입니다.

startIndex
Int32

뒤로 검색할 시작 인덱스입니다.

count
Int32

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

반환

value가 있을 경우 array에서 지정한 수의 요소를 포함하고 count에서 끝나는 startIndex의 요소 범위에서 마지막으로 검색된 값의 인덱스이고, 그렇지 않으면 배열의 하한에서 1을 뺀 값입니다.

예외

array이(가) null인 경우

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

또는

count가 0보다 작은 경우

또는

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

array가 다차원 배열인 경우

예제

다음 코드 예제에서는 배열에서 지정된 요소의 마지막 발생 인덱스 확인 하는 방법을 보여 주는 합니다. 메서드는 LastIndexOf 역방향 검색 count 이므로 가 보다 작거나 같아야 합니다(startIndex 배열의 하한을 뺀 값 1).

using namespace System;
void PrintIndexAndValues( Array^ myArray );

void main()
{
   // Creates and initializes a new Array instance with three elements of the same value.
   Array^ myArray = Array::CreateInstance( String::typeid, 12 );
   myArray->SetValue( "the", 0 );
   myArray->SetValue( "quick", 1 );
   myArray->SetValue( "brown", 2 );
   myArray->SetValue( "fox", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );
   myArray->SetValue( "in", 9 );
   myArray->SetValue( "the", 10 );
   myArray->SetValue( "barn", 11 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance contains the following values:" );
   PrintIndexAndValues( myArray );

   // Searches for the last occurrence of the duplicated value.
   String^ myString =  "the";
   int myIndex = Array::LastIndexOf( myArray, myString );
   Console::WriteLine(  "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in the first section of the Array.
   myIndex = Array::LastIndexOf( myArray, myString, 8 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in a section of the Array.  
   // Note that the start index is greater than the end index because the search is done backward.
   myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */
let printIndexAndValues (arr: 'a []) =
   for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
      printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array with three elements of the same value.
let myArray = 
   [| "the"; "quick"; "brown"; "fox"
      "jumps"; "over"; "the"; "lazy"
      "dog"; "in"; "the"; "barn" |]

// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray

// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."


//      This code produces the following output.
//
//      The Array contains the following values:
//         [0]:    the
//         [1]:    quick
//         [2]:    brown
//         [3]:    fox
//         [4]:    jumps
//         [5]:    over
//         [6]:    the
//         [7]:    lazy
//         [8]:    dog
//         [9]:    in
//         [10]:    the
//         [11]:    barn
//      The last occurrence of "the" is at index 10.
//      The last occurrence of "the" between the start and index 8 is at index 6.
//      The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces the following output.

The Array contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array with three elements of
        ' the same value.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
        myArray.SetValue("the", 0)
        myArray.SetValue("quick", 1)
        myArray.SetValue("brown", 2)
        myArray.SetValue("fox", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        myArray.SetValue("in", 9)
        myArray.SetValue("the", 10)
        myArray.SetValue("barn", 11)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintIndexAndValues(myArray)
        
        ' Searches for the last occurrence of the duplicated value.
        Dim myString As String = "the"
        Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
        Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
           myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in the first
        ' section of the Array.
        myIndex = Array.LastIndexOf(myArray, myString, 8)
        Console.WriteLine("The last occurrence of ""{0}"" between the start " _
           + "and index 8 is at index {1}.", myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in a section
        ' of the Array.  Note that the start index is greater than the end
        ' index because the search is done backward.
        myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
        Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
           + "and index 10 is at index {1}.", myString, myIndex)
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The Array contains the following values:
'     [0]:    the
'     [1]:    quick
'     [2]:    brown
'     [3]:    fox
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
'     [9]:    in
'     [10]:    the
'     [11]:    barn
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.

설명

가 0보다 큰 경우 count 1차원 Array 은 에서 startIndex 시작하여 빼기 count 플러스 1로 startIndex 끝나는 뒤로 검색됩니다.

요소는 메서드를 사용하여 지정된 값과 Object.Equals 비교됩니다. 요소 형식이 기본이 아닌(사용자 정의) 형식Equals 인 경우 해당 형식의 구현이 사용됩니다.

대부분의 배열은 하한이 0이므로 이 메서드는 가 없으면 value 일반적으로 -1을 반환합니다. 드물게 배열의 하한이 같 Int32.MinValuevalue 찾을 수 없는 경우 이 메서드는 인 를 System.Int32.MinValue - 1반환합니다Int32.MaxValue.

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

.NET Framework 2.0 이상 버전에서 이 메서드는 의 ArrayCompareTo 메서드를 사용하여 Equals 매개 변수에 value 지정된 가 Object 있는지 여부를 확인합니다. 이전 버전의 .NET Framework 자체의 valueObjectCompareTo 메서드를 Equals 사용하여 이 결정을 내렸습니다.

추가 정보

적용 대상

LastIndexOf<T>(T[], T)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

지정한 개체를 검색하여 전체 Array에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

public:
generic <typename T>
 static int LastIndexOf(cli::array <T> ^ array, T value);
public static int LastIndexOf<T> (T[] array, T value);
static member LastIndexOf : 'T[] * 'T -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T) As Integer

형식 매개 변수

T

배열 요소의 형식입니다.

매개 변수

array
T[]

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

value
T

array에서 찾을 개체입니다.

반환

전체 array 내에서 value가 있으면 마지막으로 나타나는 개체의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.

예외

array이(가) null인 경우

예제

다음 코드 예제에서는 메서드의 세 가지 제네릭 오버로드를 LastIndexOf 모두 보여 줍니다. 인덱스 위치 0 및 인덱스 위치 5에서 두 번 나타나는 하나의 항목으로 문자열 배열이 만들어집니다. LastIndexOf<T>(T[], T) 메서드 오버로드는 끝에서 전체 배열을 검색하고 문자열의 두 번째 발생을 찾습니다. LastIndexOf<T>(T[], T, Int32) 메서드 오버로드는 인덱스 위치 3부터 배열의 시작까지 배열을 뒤로 검색하고 문자열의 첫 번째 발생을 찾는 데 사용됩니다. 마지막으로, LastIndexOf<T>(T[], T, Int32, Int32) 메서드 오버로드는 인덱스 위치 4에서 시작하여 뒤로 확장하여(즉, 위치 4, 3, 2 및 1에서 항목을 검색함) 네 개의 항목 범위를 검색하는 데 사용됩니다. 이 검색은 해당 범위에 검색 문자열의 인스턴스가 없기 때문에 -1을 반환합니다.

using namespace System;

void main()
{
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };

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

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
 */
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System

let dinosaurs = 
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"

// This code example produces the following output:
//    
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1

설명

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

요소는 메서드를 사용하여 지정된 값과 Object.Equals 비교됩니다. 요소 형식이 기본이 아닌(사용자 정의) 형식 Equals 인 경우 해당 형식의 구현이 사용됩니다.

이 메서드는 O(n) 작업입니다. 여기서 n 는 의 array입니다Length.

추가 정보

적용 대상

LastIndexOf<T>(T[], T, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

지정한 개체를 검색하여 첫 번째 요소에서 지정한 인덱스로 확장하는 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

public:
generic <typename T>
 static int LastIndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int LastIndexOf<T> (T[] array, T value, int startIndex);
static member LastIndexOf : 'T[] * 'T * int -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer

형식 매개 변수

T

배열 요소의 형식입니다.

매개 변수

array
T[]

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

value
T

array에서 찾을 개체입니다.

startIndex
Int32

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

반환

첫 번째 요소에서 startIndex까지 확장하는 array의 요소 범위 내에서 value가 있으면 마지막으로 나타나는 개체의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.

예외

array이(가) null인 경우

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

예제

다음 코드 예제에서는 메서드의 세 가지 제네릭 오버로드를 LastIndexOf 모두 보여 줍니다. 인덱스 위치 0 및 인덱스 위치 5에서 두 번 나타나는 하나의 항목으로 문자열 배열이 만들어집니다. LastIndexOf<T>(T[], T) 메서드 오버로드는 끝에서 전체 배열을 검색하고 문자열의 두 번째 발생을 찾습니다. LastIndexOf<T>(T[], T, Int32) 메서드 오버로드는 인덱스 위치 3부터 배열의 시작까지 배열을 뒤로 검색하고 문자열의 첫 번째 발생을 찾는 데 사용됩니다. 마지막으로, LastIndexOf<T>(T[], T, Int32, Int32) 메서드 오버로드는 인덱스 위치 4에서 시작하여 뒤로 확장하여(즉, 위치 4, 3, 2 및 1에서 항목을 검색함) 네 개의 항목 범위를 검색하는 데 사용됩니다. 이 검색은 해당 범위에 검색 문자열의 인스턴스가 없기 때문에 -1을 반환합니다.

using namespace System;

void main()
{
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };

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

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
 */
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System

let dinosaurs = 
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"

// This code example produces the following output:
//    
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1

설명

Array 에서 시작하여 첫 번째 요소에서 startIndex 끝나는 뒤로 검색됩니다.

요소는 메서드를 사용하여 지정된 값과 Object.Equals 비교됩니다. 요소 형식이 기본이 아닌(사용자 정의) 형식 Equals 인 경우 해당 형식의 구현이 사용됩니다.

이 메서드는 O(n) 연산입니다. 여기서 n 는 의 arraystartIndex시작 부분에서 부터 로의 요소 수입니다.

추가 정보

적용 대상

LastIndexOf<T>(T[], T, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

지정한 개체를 검색하여 지정한 수의 요소를 포함하고 지정한 인덱스에서 끝나는 Array 의 요소 범위에서 마지막으로 검색된 요소의 인덱스를 반환합니다.

public:
generic <typename T>
 static int LastIndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int LastIndexOf<T> (T[] array, T value, int startIndex, int count);
static member LastIndexOf : 'T[] * 'T * int * int -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T, startIndex As Integer, count As Integer) As Integer

형식 매개 변수

T

배열 요소의 형식입니다.

매개 변수

array
T[]

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

value
T

array에서 찾을 개체입니다.

startIndex
Int32

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

count
Int32

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

반환

count에서 지정한 수의 요소를 포함하고 startIndex에서 끝나는 array의 요소 범위 내에서 value가 있으면 마지막으로 나타나는 개체의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.

예외

array이(가) null인 경우

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

또는

count가 0보다 작은 경우

또는

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

예제

다음 코드 예제에서는 메서드의 세 가지 제네릭 오버로드를 LastIndexOf 모두 보여 줍니다. 인덱스 위치 0 및 인덱스 위치 5에서 두 번 나타나는 하나의 항목으로 문자열 배열이 만들어집니다. LastIndexOf<T>(T[], T) 메서드 오버로드는 끝에서 전체 배열을 검색하고 문자열의 두 번째 발생을 찾습니다. LastIndexOf<T>(T[], T, Int32) 메서드 오버로드는 인덱스 위치 3부터 배열의 시작까지 배열을 뒤로 검색하고 문자열의 첫 번째 발생을 찾는 데 사용됩니다. 마지막으로, LastIndexOf<T>(T[], T, Int32, Int32) 메서드 오버로드는 인덱스 위치 4에서 시작하여 뒤로 확장하여(즉, 위치 4, 3, 2 및 1에서 항목을 검색함) 네 개의 항목 범위를 검색하는 데 사용됩니다. 이 검색은 해당 범위에 검색 문자열의 인스턴스가 없기 때문에 -1을 반환합니다.

using namespace System;

void main()
{
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };

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

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
 */
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System

let dinosaurs = 
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"

// This code example produces the following output:
//    
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1

설명

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

요소는 메서드를 사용하여 지정된 값과 Object.Equals 비교됩니다. 요소 형식이 기본이 아닌(사용자 정의) 형식 Equals 인 경우 해당 형식의 구현이 사용됩니다.

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

추가 정보

적용 대상