Array.LastIndexOf Array.LastIndexOf Array.LastIndexOf Array.LastIndexOf Method

定義

傳回一維 ArrayArray 的某部分中數值的最後一個相符項目索引。Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.

多載

LastIndexOf(Array, Object) LastIndexOf(Array, Object) LastIndexOf(Array, Object)

搜尋指定物件,並且傳回整個一維 Array 中最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

LastIndexOf(Array, Object, Int32) LastIndexOf(Array, Object, Int32) LastIndexOf(Array, Object, Int32)

搜尋指定的物件,並傳回一維 Array 內從第一個項目延伸至指定之索引的項目範圍中,最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that extends from the first element to the specified index.

LastIndexOf(Array, Object, Int32, Int32) LastIndexOf(Array, Object, Int32, Int32) LastIndexOf(Array, Object, Int32, Int32)

搜尋指定的物件,並傳回一維 Array 中包含指定之項目數且結束於指定之索引的項目範圍內,最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that contains the specified number of elements and ends at the specified index.

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

搜尋指定的物件,並傳回整個 Array 中最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the entire Array.

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

Array 中從第一個項目開始到指定的索引這段範圍的項目內,搜尋指定的物件最後一次出現的位置,並傳回其索引值 (索引以零為起始)。Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index.

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

搜尋指定的物件,並傳回 Array 中包含指定之項目數且結束於指定之索引的項目範圍內,最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.

LastIndexOf(Array, Object) LastIndexOf(Array, Object) LastIndexOf(Array, Object)

搜尋指定物件,並且傳回整個一維 Array 中最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

public:
 static int LastIndexOf(Array ^ array, System::Object ^ value);
public static int LastIndexOf (Array array, object value);
static member LastIndexOf : Array * obj -> int

參數

array
Array Array Array Array

要搜尋的一維 ArrayThe one-dimensional Array to search.

value
Object Object Object Object

要在 array 中尋找的物件。The object to locate in array.

傳回

如有找到,則為整個 arrayvalue 最後一個相符項目的索引,否則為陣列的下限減 1。The index of the last occurrence of value within the entire array, if found; otherwise, the lower bound of the array minus 1.

例外狀況

array 是多維的。array is multidimensional.

範例

下列程式碼範例示範如何判斷指定的項目,在陣列中最後一個出現的索引。The following code example shows how to determine the index of the last occurrence of a specified element in an 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.
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // 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 );
   }


   public static 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 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.
*/
Imports System
Imports Microsoft.VisualBasic

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. 

備註

一維Array向後最後一個項目開始和結束時間的第一個項目搜尋。The one-dimensional Array is searched backward starting at the last element and ending at the first element.

項目會比較指定的值使用Object.Equals方法。The elements are compared to the specified value using the Object.Equals method. 如果項目類型為非內建 (使用者定義) 的類型,Equals會使用該類型的實作。If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

由於大部分的陣列將會有下限為零,這個方法通常會傳回-1 時value找不到。Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. 在罕見的情況下,陣列的下限是等於Int32.MinValuevalue找不到,這個方法會傳回Int32.MaxValue,也就是System.Int32.MinValue - 1In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

這個方法是 O (n) 運算,其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

在.NET Framework 2.0 版中,這個方法會使用EqualsCompareTo方法Array來判斷是否Object所指定value參數存在。In the .NET Framework version 2.0, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. 在舊版的.NET Framework 中,藉由進行這項判斷EqualsCompareTovalueObject本身。In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

CompareTo 方法的item參數集合中的物件。CompareTo methods of the item parameter on the objects in the collection.

另請參閱

LastIndexOf(Array, Object, Int32) LastIndexOf(Array, Object, Int32) LastIndexOf(Array, Object, Int32)

搜尋指定的物件,並傳回一維 Array 內從第一個項目延伸至指定之索引的項目範圍中,最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that extends from the first element to the specified index.

public:
 static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int LastIndexOf (Array array, object value, int startIndex);
static member LastIndexOf : Array * obj * int -> int

參數

array
Array Array Array Array

要搜尋的一維 ArrayThe one-dimensional Array to search.

value
Object Object Object Object

要在 array 中尋找的物件。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

反向搜尋的起始索引。The starting index of the backward search.

傳回

如有找到,則為 array 中從第一個項目延伸到 startIndex 的項目範圍內,value 的最後一個相符項目的索引,否則為陣列的下限減 1。The index of the last occurrence of value within the range of elements in array that extends from the first element to startIndex, if found; otherwise, the lower bound of the array minus 1.

例外狀況

startIndex 超出 array 的有效索引範圍。startIndex is outside the range of valid indexes for array.

array 是多維的。array is multidimensional.

範例

下列程式碼範例示範如何判斷指定的項目,在陣列中最後一個出現的索引。The following code example shows how to determine the index of the last occurrence of a specified element in an 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.
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // 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 );
   }


   public static 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 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.
*/
Imports System
Imports Microsoft.VisualBasic

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. 

備註

一維Array會搜尋回溯開始startIndex和結束時間的第一個元素。The one-dimensional Array is searched backward starting at startIndex and ending at the first element.

項目會比較指定的值使用Object.Equals方法。The elements are compared to the specified value using the Object.Equals method. 如果項目類型為非內建 (使用者定義) 的類型,Equals會使用該類型的實作。If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

由於大部分的陣列將會有下限為零,這個方法通常會傳回-1 時value找不到。Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. 在罕見的情況下,陣列的下限是等於Int32.MinValuevalue找不到,這個方法會傳回Int32.MaxValue,也就是System.Int32.MinValue - 1In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

這個方法是 O (n) 運算,其中n是從開頭的項目數arraystartIndexThis method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

在.NET Framework 2.0 版中,這個方法會使用EqualsCompareTo方法Array來判斷是否Object所指定value參數存在。In the .NET Framework version 2.0, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. 在舊版的.NET Framework 中,藉由進行這項判斷EqualsCompareTovalueObject本身。In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

另請參閱

LastIndexOf(Array, Object, Int32, Int32) LastIndexOf(Array, Object, Int32, Int32) LastIndexOf(Array, Object, Int32, Int32)

搜尋指定的物件,並傳回一維 Array 中包含指定之項目數且結束於指定之索引的項目範圍內,最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that contains the specified number of elements and ends at the specified index.

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);
static member LastIndexOf : Array * obj * int * int -> int

參數

array
Array Array Array Array

要搜尋的一維 ArrayThe one-dimensional Array to search.

value
Object Object Object Object

要在 array 中尋找的物件。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

反向搜尋的起始索引。The starting index of the backward search.

count
Int32 Int32 Int32 Int32

區段中要搜尋的項目數目。The number of elements in the section to search.

傳回

如有找到,則為在 array 中包含 count 內指定之項目數且結束於 startIndex 的項目範圍內,value 的最後一個相符項目之索引,否則為陣列的下限減 1。The index of the last occurrence of value within the range of elements in array that contains the number of elements specified in count and ends at startIndex, if found; otherwise, the lower bound of the array minus 1.

例外狀況

startIndex 超出 array 的有效索引範圍。startIndex is outside the range of valid indexes for array.

-或--or- count 小於零。count is less than zero.

-或--or- startIndexcount 未指定 array 中的有效區段。startIndex and count do not specify a valid section in array.

array 是多維的。array is multidimensional.

範例

下列程式碼範例示範如何判斷指定的項目,在陣列中最後一個出現的索引。The following code example shows how to determine the index of the last occurrence of a specified element in an array. 請注意,LastIndexOf方法是向後搜尋,因此count必須是小於或等於 (startIndex陣列加 1 的下限減)。Note that the LastIndexOf method is a backward search; therefore, count must be less than or equal to (startIndex minus the lower bound of the array plus 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.
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // 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 );
   }


   public static 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 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.
*/
Imports System
Imports Microsoft.VisualBasic

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. 

備註

一維Array會搜尋回溯開始startIndex結尾startIndex減去count再加上 1,如果count大於 0。The one-dimensional Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

項目會比較指定的值使用Object.Equals方法。The elements are compared to the specified value using the Object.Equals method. 如果項目類型為非內建 (使用者定義) 的類型,Equals會使用該類型的實作。If the element type is a nonintrinsic (user-defined) type, theEquals implementation of that type is used.

由於大部分的陣列將會有下限為零,這個方法通常會傳回-1 時value找不到。Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. 在罕見的情況下,陣列的下限是等於Int32.MinValuevalue找不到,這個方法會傳回Int32.MaxValue,也就是System.Int32.MinValue - 1In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

這個方法是 O (n) 運算,其中ncountThis method is an O(n) operation, where n is count.

在.NET Framework 2.0 版中,這個方法會使用EqualsCompareTo方法Array來判斷是否Object所指定value參數存在。In the .NET Framework version 2.0, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. 在舊版的.NET Framework 中,藉由進行這項判斷EqualsCompareTovalueObject本身。In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

另請參閱

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

搜尋指定的物件,並傳回整個 Array 中最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the entire 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

陣列項目的類型。The type of the elements of the array.

參數

array
T[]

要搜尋的一維且以零為起始的 ArrayThe one-dimensional, zero-based Array to search.

value
T T T T

要在 array 中尋找的物件。The object to locate in array.

傳回

整個 array 中最後一次出現 value 之以零為起始的索引;如未找到則為 -1。The zero-based index of the last occurrence of value within the entire array, if found; otherwise, -1.

例外狀況

範例

下列程式碼範例示範三個泛用的所有多載LastIndexOf方法。The following code example demonstrates all three generic overloads of the LastIndexOf method. 建立字串陣列,會出現兩次,在索引位置 0 和 5 的索引位置的一個項目。An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. LastIndexOf<T>(T[], T)方法多載從結束時,整個陣列中搜尋和尋找字串的第二個項目。The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. LastIndexOf<T>(T[], T, Int32)方法多載用來搜尋陣列回溯開頭的索引位置為 3,一直持續到陣列中的開頭,並尋找字串的第一個項目。The LastIndexOf<T>(T[], T, Int32) method overload is used to search the array backward beginning with index location 3 and continuing to the beginning of the array, and finds the first occurrence of the string. 最後,LastIndexOf<T>(T[], T, Int32, Int32)方法多載用來搜尋四個項目,索引位置 4 開始,並向後擴充的範圍 (也就是說,它會搜尋在位置 4、 3、 2 和 1 的項目); 此搜尋會傳回-1,因為沒有搜尋的執行個體在該範圍內的字串。Finally, the LastIndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of four entries, beginning at index location 4 and extending backward (that is, it searches the items at locations 4, 3, 2, and 1); this search returns -1 because there are no instances of the search string in that range.

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
 */
using System;

public class Example
{
    public static void Main()
    {
        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
 */
Imports System

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向後最後一個項目開始和結束時間的第一個項目搜尋。The Array is searched backward starting at the last element and ending at the first element.

項目會比較指定的值使用Object.Equals方法。The elements are compared to the specified value using the Object.Equals method. 如果項目類型為非內建 (使用者定義) 的類型,Equals會使用該類型的實作。If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

這個方法是 O (n) 運算,其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

另請參閱

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

Array 中從第一個項目開始到指定的索引這段範圍的項目內,搜尋指定的物件最後一次出現的位置,並傳回其索引值 (索引以零為起始)。Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index.

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

陣列項目的類型。The type of the elements of the array.

參數

array
T[]

要搜尋的一維且以零為起始的 ArrayThe one-dimensional, zero-based Array to search.

value
T T T T

要在 array 中尋找的物件。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

向後搜尋之以零為起始的起始索引。The zero-based starting index of the backward search.

傳回

array 中從第一個項目到 startIndex 的項目範圍內,最後一次出現 value 之以零為起始的索引;如未找到則為 -1。The zero-based index of the last occurrence of value within the range of elements in array that extends from the first element to startIndex, if found; otherwise, -1.

例外狀況

startIndex 超出 array 的有效索引範圍。startIndex is outside the range of valid indexes for array.

範例

下列程式碼範例示範三個泛用的所有多載LastIndexOf方法。The following code example demonstrates all three generic overloads of the LastIndexOf method. 建立字串陣列,會出現兩次,在索引位置 0 和 5 的索引位置的一個項目。An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. LastIndexOf<T>(T[], T)方法多載從結束時,整個陣列中搜尋和尋找字串的第二個項目。The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. LastIndexOf<T>(T[], T, Int32)方法多載用來搜尋陣列回溯開頭的索引位置為 3,一直持續到陣列中的開頭,並尋找字串的第一個項目。The LastIndexOf<T>(T[], T, Int32) method overload is used to search the array backward beginning with index location 3 and continuing to the beginning of the array, and finds the first occurrence of the string. 最後,LastIndexOf<T>(T[], T, Int32, Int32)方法多載用來搜尋四個項目,索引位置 4 開始,並向後擴充的範圍 (也就是說,它會搜尋在位置 4、 3、 2 和 1 的項目); 此搜尋會傳回-1,因為沒有搜尋的執行個體在該範圍內的字串。Finally, the LastIndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of four entries, beginning at index location 4 and extending backward (that is, it searches the items at locations 4, 3, 2, and 1); this search returns -1 because there are no instances of the search string in that range.

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
 */
using System;

public class Example
{
    public static void Main()
    {
        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
 */
Imports System

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和結束時間的第一個元素。The Array is searched backward starting at startIndex and ending at the first element.

項目會比較指定的值使用Object.Equals方法。The elements are compared to the specified value using the Object.Equals method. 如果項目類型為非內建 (使用者定義) 的類型,Equals會使用該類型的實作。If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

這個方法是 O (n) 運算,其中n是從開頭的項目數arraystartIndexThis method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

另請參閱

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

搜尋指定的物件,並傳回 Array 中包含指定之項目數且結束於指定之索引的項目範圍內,最後一個相符項目的索引。Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.

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

陣列項目的類型。The type of the elements of the array.

參數

array
T[]

要搜尋的一維且以零為起始的 ArrayThe one-dimensional, zero-based Array to search.

value
T T T T

要在 array 中尋找的物件。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

向後搜尋之以零為起始的起始索引。The zero-based starting index of the backward search.

count
Int32 Int32 Int32 Int32

區段中要搜尋的項目數目。The number of elements in the section to search.

傳回

array 中包含 count 中指定項目數且結束於 startIndex 的項目範圍內,最後一次出現 value 之以零為起始的索引;如未找到則為 -1。The zero-based index of the last occurrence of value within the range of elements in array that contains the number of elements specified in count and ends at startIndex, if found; otherwise, -1.

例外狀況

startIndex 超出 array 的有效索引範圍。startIndex is outside the range of valid indexes for array.

-或--or- count 小於零。count is less than zero.

-或--or- startIndexcount 未指定 array 中的有效區段。startIndex and count do not specify a valid section in array.

範例

下列程式碼範例示範三個泛用的所有多載LastIndexOf方法。The following code example demonstrates all three generic overloads of the LastIndexOf method. 建立字串陣列,會出現兩次,在索引位置 0 和 5 的索引位置的一個項目。An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. LastIndexOf<T>(T[], T)方法多載從結束時,整個陣列中搜尋和尋找字串的第二個項目。The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. LastIndexOf<T>(T[], T, Int32)方法多載用來搜尋陣列回溯開頭的索引位置為 3,一直持續到陣列中的開頭,並尋找字串的第一個項目。The LastIndexOf<T>(T[], T, Int32) method overload is used to search the array backward beginning with index location 3 and continuing to the beginning of the array, and finds the first occurrence of the string. 最後,LastIndexOf<T>(T[], T, Int32, Int32)方法多載用來搜尋四個項目,索引位置 4 開始,並向後擴充的範圍 (也就是說,它會搜尋在位置 4、 3、 2 和 1 的項目); 此搜尋會傳回-1,因為沒有搜尋的執行個體在該範圍內的字串。Finally, the LastIndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of four entries, beginning at index location 4 and extending backward (that is, it searches the items at locations 4, 3, 2, and 1); this search returns -1 because there are no instances of the search string in that range.

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
 */
using System;

public class Example
{
    public static void Main()
    {
        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
 */
Imports System

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結尾startIndex減去count再加上 1,如果count大於 0。The Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

項目會比較指定的值使用Object.Equals方法。The elements are compared to the specified value using the Object.Equals method. 如果項目類型為非內建 (使用者定義) 的類型,Equals會使用該類型的實作。If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

這個方法是 O (n) 運算,其中ncountThis method is an O(n) operation, where n is count.

另請參閱

適用於