Array.LastIndexOf Metoda

Definicja

Zwraca indeks ostatniego wystąpienia wartości w jednowymiarowej Array lub w części Array.Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.

Przeciążenia

LastIndexOf(Array, Object)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w obrębie całego Array jednowymiarowego.Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

LastIndexOf(Array, Object, Int32)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowej Array, który rozciąga się od pierwszego elementu do określonego indeksu.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)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowej Array, która zawiera określoną liczbę elementów i zostanie zakończona w określonym indeksie.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)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w całym Array.Searches for the specified object and returns the index of the last occurrence within the entire Array.

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

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w Array, który rozciąga się od pierwszego elementu do określonego indeksu.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)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w Array, który zawiera określoną liczbę elementów i kończą się o określonym indeksie.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)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w obrębie całego Array jednowymiarowego.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

Parametry

array
Array

Array Jednowymiarowa do przeszukania.The one-dimensional Array to search.

value
Object

Obiekt, który ma zostać zlokalizowany w array.The object to locate in array.

Zwraca

Indeks ostatniego wystąpienia value w całej array, jeśli znaleziono; w przeciwnym razie Dolna granica tablicy minus 1.The index of the last occurrence of value within the entire array, if found; otherwise, the lower bound of the array minus 1.

Wyjątki

array jest null.array is null.

array jest wielowymiarowych.array is multidimensional.

Przykłady

Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu w tablicy.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.
*/
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. 

Uwagi

Array jednowymiarowe jest przeszukiwany wstecz, zaczynając od ostatniego elementu i kończąc na pierwszym elemencie.The one-dimensional Array is searched backward starting at the last element and ending at the first element.

Elementy są porównywane z określoną wartością przy użyciu metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Jeśli typ elementu jest typem niewewnętrznym (zdefiniowanym przez użytkownika), używana jest implementacja Equals tego typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Ponieważ większość tablic będzie miała dolną granicę równą zero, ta metoda zwykle zwróci wartość-1, jeśli nie zostanie znaleziona value.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. W rzadkich przypadkach Dolna granica tablicy jest równa Int32.MinValue i nie znaleziono value, Metoda ta zwraca Int32.MaxValue, który jest System.Int32.MinValue - 1.In 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.

Ta metoda jest operacją O (n), gdzie n jest Length array.This method is an O(n) operation, where n is the Length of array.

W .NET Framework w wersji 2,0 metoda używa metody Equals i CompareTo Array, aby określić, czy istnieje Object określony przez parametr 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. We wcześniejszych wersjach .NET Framework to oznaczanie zostało wykonane przy użyciu metod Equals i CompareTo samej valueObject.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

CompareTo metody item parametru dla obiektów w kolekcji.CompareTo methods of the item parameter on the objects in the collection.

Zobacz też

LastIndexOf(Array, Object, Int32)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowej Array, który rozciąga się od pierwszego elementu do określonego indeksu.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

Parametry

array
Array

Array Jednowymiarowa do przeszukania.The one-dimensional Array to search.

value
Object

Obiekt, który ma zostać zlokalizowany w array.The object to locate in array.

startIndex
Int32

Początkowy indeks wyszukiwania wstecznego.The starting index of the backward search.

Zwraca

Indeks ostatniego wystąpienia value w zakresie elementów w array, który rozciąga się od pierwszego elementu do startIndex, jeśli został znaleziony; w przeciwnym razie Dolna granica tablicy minus 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.

Wyjątki

array jest null.array is null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla array.startIndex is outside the range of valid indexes for array.

array jest wielowymiarowych.array is multidimensional.

Przykłady

Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu w tablicy.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.
*/
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. 

Uwagi

Array jednowymiarowe jest przeszukiwany do tyłu w odróżnieniu od startIndex i kończący się na pierwszym elemencie.The one-dimensional Array is searched backward starting at startIndex and ending at the first element.

Elementy są porównywane z określoną wartością przy użyciu metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Jeśli typ elementu jest typem niewewnętrznym (zdefiniowanym przez użytkownika), używana jest implementacja Equals tego typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Ponieważ większość tablic będzie miała dolną granicę równą zero, ta metoda zwykle zwróci wartość-1, jeśli nie zostanie znaleziona value.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. W rzadkich przypadkach Dolna granica tablicy jest równa Int32.MinValue i nie znaleziono value, Metoda ta zwraca Int32.MaxValue, który jest System.Int32.MinValue - 1.In 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.

Ta metoda jest operacją O (n), gdzie n jest liczbą elementów od początku array do startIndex.This method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

W .NET Framework w wersji 2,0 metoda używa metody Equals i CompareTo Array, aby określić, czy istnieje Object określony przez parametr 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. We wcześniejszych wersjach .NET Framework to oznaczanie zostało wykonane przy użyciu metod Equals i CompareTo samej valueObject.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

Zobacz też

LastIndexOf(Array, Object, Int32, Int32)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowej Array, która zawiera określoną liczbę elementów i zostanie zakończona w określonym indeksie.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

Parametry

array
Array

Array Jednowymiarowa do przeszukania.The one-dimensional Array to search.

value
Object

Obiekt, który ma zostać zlokalizowany w array.The object to locate in array.

startIndex
Int32

Początkowy indeks wyszukiwania wstecznego.The starting index of the backward search.

count
Int32

Liczba elementów w sekcji do wyszukania.The number of elements in the section to search.

Zwraca

Indeks ostatniego wystąpienia value w zakresie elementów w array, który zawiera liczbę elementów określoną w count i zostaje zakończony przy startIndex, jeśli znaleziono; w przeciwnym razie Dolna granica tablicy minus 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.

Wyjątki

array jest null.array is null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla array.startIndex is outside the range of valid indexes for array.

—lub—-or- count jest mniejsza od zera.count is less than zero.

—lub—-or- startIndex i count nie określają prawidłowej sekcji w array.startIndex and count do not specify a valid section in array.

array jest wielowymiarowych.array is multidimensional.

Przykłady

Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu w tablicy.The following code example shows how to determine the index of the last occurrence of a specified element in an array. Należy pamiętać, że metoda LastIndexOf jest wyszukiwaniem wstecznym; w związku z tym count muszą być mniejsze lub równe (startIndex minus Dolna granica tablicy plus 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.
*/
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. 

Uwagi

Array jednowymiarowe jest przeszukiwany do tyłu w odróżnieniu od startIndex i kończące się na startIndex minus count plus 1, jeśli count jest większa od 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.

Elementy są porównywane z określoną wartością przy użyciu metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Jeśli typ elementu jest typem niewewnętrznym (zdefiniowanym przez użytkownika), używana jest implementacja Equals tego typu.If the element type is a nonintrinsic (user-defined) type, theEquals implementation of that type is used.

Ponieważ większość tablic będzie miała dolną granicę równą zero, ta metoda zwykle zwróci wartość-1, jeśli nie zostanie znaleziona value.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. W rzadkich przypadkach Dolna granica tablicy jest równa Int32.MinValue i nie znaleziono value, Metoda ta zwraca Int32.MaxValue, który jest System.Int32.MinValue - 1.In 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.

Ta metoda jest operacją O (n), gdzie n jest count.This method is an O(n) operation, where n is count.

W .NET Framework w wersji 2,0 metoda używa metody Equals i CompareTo Array, aby określić, czy istnieje Object określony przez parametr 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. We wcześniejszych wersjach .NET Framework to oznaczanie zostało wykonane przy użyciu metod Equals i CompareTo samej valueObject.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

Zobacz też

LastIndexOf<T>(T[], T)

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w całym 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

Parametry typu

T

Typ elementów tablicy.The type of the elements of the array.

Parametry

array
T[]

Jednowymiarowe, oparte na zero Array do wyszukania.The one-dimensional, zero-based Array to search.

value
T

Obiekt, który ma zostać zlokalizowany w array.The object to locate in array.

Zwraca

Indeks (liczony od zera) ostatniego wystąpienia value w całej array, jeśli znaleziono; w przeciwnym razie-1.The zero-based index of the last occurrence of value within the entire array, if found; otherwise, -1.

Wyjątki

array jest null.array is null.

Przykłady

Poniższy przykład kodu demonstruje wszystkie trzy ogólne przeciążenia metody LastIndexOf.The following code example demonstrates all three generic overloads of the LastIndexOf method. Tablica ciągów jest tworzona, gdy jeden wpis, to pojawia się dwukrotnie, z lokalizacją indeksu 0 i lokalizacją indeksu 5.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Przeciążenie metody LastIndexOf<T>(T[], T) przeszukuje całą tablicę od końca i znajduje drugie wystąpienie ciągu.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Przeciążenie metody LastIndexOf<T>(T[], T, Int32) jest używany do przeszukiwania tablicy do tyłu od lokalizacji indeksu 3 i kontynuowania na początku tablicy i znajdowania pierwszego wystąpienia ciągu.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. Na koniec Przeciążenie metody LastIndexOf<T>(T[], T, Int32, Int32) jest używany do wyszukiwania zakresu czterech wpisów, rozpoczynając od lokalizacji indeksu 4 i rozszerzania do tyłu (to znaczy przeszukuje elementy w lokalizacjach 4, 3, 2 i 1); to wyszukiwanie zwraca wartość-1, ponieważ w tym zakresie nie ma wystąpień ciągu wyszukiwania.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
 */
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

Uwagi

Array jest przeszukiwany wstecz od ostatniego elementu i kończący się na pierwszym elemencie.The Array is searched backward starting at the last element and ending at the first element.

Elementy są porównywane z określoną wartością przy użyciu metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Jeśli typ elementu jest typem niewewnętrznym (zdefiniowanym przez użytkownika), używana jest implementacja Equals tego typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Ta metoda jest operacją O (n), gdzie n jest Length array.This method is an O(n) operation, where n is the Length of array.

Zobacz też

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

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w Array, który rozciąga się od pierwszego elementu do określonego indeksu.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

Parametry typu

T

Typ elementów tablicy.The type of the elements of the array.

Parametry

array
T[]

Jednowymiarowe, oparte na zero Array do wyszukania.The one-dimensional, zero-based Array to search.

value
T

Obiekt, który ma zostać zlokalizowany w array.The object to locate in array.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.The zero-based starting index of the backward search.

Zwraca

Indeks (liczony od zera) ostatniego wystąpienia value w zakresie elementów w array, który rozciąga się od pierwszego elementu do startIndex, jeśli znaleziono; w przeciwnym razie-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.

Wyjątki

array jest null.array is null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla array.startIndex is outside the range of valid indexes for array.

Przykłady

Poniższy przykład kodu demonstruje wszystkie trzy ogólne przeciążenia metody LastIndexOf.The following code example demonstrates all three generic overloads of the LastIndexOf method. Tablica ciągów jest tworzona, gdy jeden wpis, to pojawia się dwukrotnie, z lokalizacją indeksu 0 i lokalizacją indeksu 5.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Przeciążenie metody LastIndexOf<T>(T[], T) przeszukuje całą tablicę od końca i znajduje drugie wystąpienie ciągu.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Przeciążenie metody LastIndexOf<T>(T[], T, Int32) jest używany do przeszukiwania tablicy do tyłu od lokalizacji indeksu 3 i kontynuowania na początku tablicy i znajdowania pierwszego wystąpienia ciągu.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. Na koniec Przeciążenie metody LastIndexOf<T>(T[], T, Int32, Int32) jest używany do wyszukiwania zakresu czterech wpisów, rozpoczynając od lokalizacji indeksu 4 i rozszerzania do tyłu (to znaczy przeszukuje elementy w lokalizacjach 4, 3, 2 i 1); to wyszukiwanie zwraca wartość-1, ponieważ w tym zakresie nie ma wystąpień ciągu wyszukiwania.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
 */
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

Uwagi

Array jest przeszukiwany do tyłu w odróżnieniu od startIndex i kończący się na pierwszym elemencie.The Array is searched backward starting at startIndex and ending at the first element.

Elementy są porównywane z określoną wartością przy użyciu metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Jeśli typ elementu jest typem niewewnętrznym (zdefiniowanym przez użytkownika), używana jest implementacja Equals tego typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Ta metoda jest operacją O (n), gdzie n jest liczbą elementów od początku array do startIndex.This method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

Zobacz też

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

Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w Array, który zawiera określoną liczbę elementów i kończą się o określonym indeksie.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

Parametry typu

T

Typ elementów tablicy.The type of the elements of the array.

Parametry

array
T[]

Jednowymiarowe, oparte na zero Array do wyszukania.The one-dimensional, zero-based Array to search.

value
T

Obiekt, który ma zostać zlokalizowany w array.The object to locate in array.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.The zero-based starting index of the backward search.

count
Int32

Liczba elementów w sekcji do wyszukania.The number of elements in the section to search.

Zwraca

Indeks (liczony od zera) ostatniego wystąpienia value w zakresie elementów w array, który zawiera liczbę elementów określoną w count i zostaje zakończony w startIndex, jeśli znaleziono; w przeciwnym razie-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.

Wyjątki

array jest null.array is null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla array.startIndex is outside the range of valid indexes for array.

—lub—-or- count jest mniejsza od zera.count is less than zero.

—lub—-or- startIndex i count nie określają prawidłowej sekcji w array.startIndex and count do not specify a valid section in array.

Przykłady

Poniższy przykład kodu demonstruje wszystkie trzy ogólne przeciążenia metody LastIndexOf.The following code example demonstrates all three generic overloads of the LastIndexOf method. Tablica ciągów jest tworzona, gdy jeden wpis, to pojawia się dwukrotnie, z lokalizacją indeksu 0 i lokalizacją indeksu 5.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Przeciążenie metody LastIndexOf<T>(T[], T) przeszukuje całą tablicę od końca i znajduje drugie wystąpienie ciągu.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Przeciążenie metody LastIndexOf<T>(T[], T, Int32) jest używany do przeszukiwania tablicy do tyłu od lokalizacji indeksu 3 i kontynuowania na początku tablicy i znajdowania pierwszego wystąpienia ciągu.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. Na koniec Przeciążenie metody LastIndexOf<T>(T[], T, Int32, Int32) jest używany do wyszukiwania zakresu czterech wpisów, rozpoczynając od lokalizacji indeksu 4 i rozszerzania do tyłu (to znaczy przeszukuje elementy w lokalizacjach 4, 3, 2 i 1); to wyszukiwanie zwraca wartość-1, ponieważ w tym zakresie nie ma wystąpień ciągu wyszukiwania.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
 */
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

Uwagi

Array jest przeszukiwany w tył, zaczynając od startIndex i kończący się na startIndex minus count plus 1, jeśli count jest większa od 0.The Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

Elementy są porównywane z określoną wartością przy użyciu metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Jeśli typ elementu jest typem niewewnętrznym (zdefiniowanym przez użytkownika), używana jest implementacja Equals tego typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Ta metoda jest operacją O (n), gdzie n jest count.This method is an O(n) operation, where n is count.

Zobacz też

Dotyczy