Array.LastIndexOf Methode

Definition

Gibt den Index des letzten Vorkommens eines Werts in einem eindimensionalen Array oder in einem Teil des Array zurück.Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.

Überlädt

LastIndexOf(Array, Object)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des gesamten eindimensionalen Array zurück.Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

LastIndexOf(Array, Object, Int32)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im eindimensionalen Array zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.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)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im eindimensionalen Array zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.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)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des gesamten Array zurück.Searches for the specified object and returns the index of the last occurrence within the entire Array.

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

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im Array zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.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)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im Array zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.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)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des gesamten eindimensionalen Array zurück.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

Parameter

array
Array

Das zu durchsuchende eindimensionale Array.The one-dimensional Array to search.

value
Object

Das in array zu suchende Objekt.The object to locate in array.

Gibt zurück

Der Index des letzten Vorkommens von value im gesamten array, sofern gefunden, andernfalls die untere Grenze des Arrays 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.

Ausnahmen

array ist null.array is null.

array ist mehrdimensional.array is multidimensional.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie der Index des letzten Vorkommens eines angegebenen Elements in einem Array bestimmt wird.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. 

Hinweise

Das eindimensionale Array wird rückwärts durchsucht, beginnend beim letzten Element und endet beim ersten Element.The one-dimensional Array is searched backward starting at the last element and ending at the first element.

Die Elemente werden mit dem angegebenen Wert mit der Object.Equals-Methode verglichen.The elements are compared to the specified value using the Object.Equals method. Wenn der Elementtyp ein nicht System interner (benutzerdefinierter) Typ ist, wird die Equals-Implementierung dieses Typs verwendet.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Da die meisten Arrays eine untere Grenze von 0 (null) aufweisen, gibt diese Methode in der Regel-1 zurück, wenn value nicht gefunden wird.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. In dem seltenen Fall, dass die untere Grenze des Arrays gleich Int32.MinValue und value nicht gefunden wird, gibt diese Methode Int32.MaxValue zurück, das System.Int32.MinValue - 1 ist.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.

Bei dieser Methode handelt es sich um einen O (n)-Vorgang, bei dem n der Length von array ist.This method is an O(n) operation, where n is the Length of array.

In der .NET Framework-Version 2,0 verwendet diese Methode die Methoden Equals und CompareTo der Array, um zu bestimmen, ob das durch den value-Parameter angegebene Object vorhanden ist.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. In früheren Versionen der .NET Framework wurde diese Bestimmung mithilfe der Methoden Equals und CompareTo der valueObject selbst hergestellt.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

CompareTo-Methoden des item-Parameters für die Objekte in der Auflistung.CompareTo methods of the item parameter on the objects in the collection.

Siehe auch

LastIndexOf(Array, Object, Int32)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im eindimensionalen Array zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.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

Parameter

array
Array

Das zu durchsuchende eindimensionale Array.The one-dimensional Array to search.

value
Object

Das in array zu suchende Objekt.The object to locate in array.

startIndex
Int32

Der Startindex für die Rückwärtssuche.The starting index of the backward search.

Gibt zurück

Der Index des letzten Vorkommens von value innerhalb des Bereichs von Elementen im array, das sich vom ersten Element bis zum startIndex erstreckt, sofern gefunden, andernfalls die untere Grenze des Arrays 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.

Ausnahmen

array ist null.array is null.

startIndex liegt außerhalb des Bereichs der gültigen Indizes für array.startIndex is outside the range of valid indexes for array.

array ist mehrdimensional.array is multidimensional.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie der Index des letzten Vorkommens eines angegebenen Elements in einem Array bestimmt wird.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. 

Hinweise

Der eindimensionale Array wird rückwärts durchsucht, beginnend bei startIndex und endet beim ersten Element.The one-dimensional Array is searched backward starting at startIndex and ending at the first element.

Die Elemente werden mit dem angegebenen Wert mit der Object.Equals-Methode verglichen.The elements are compared to the specified value using the Object.Equals method. Wenn der Elementtyp ein nicht System interner (benutzerdefinierter) Typ ist, wird die Equals-Implementierung dieses Typs verwendet.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Da die meisten Arrays eine untere Grenze von 0 (null) aufweisen, gibt diese Methode in der Regel-1 zurück, wenn value nicht gefunden wird.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. In dem seltenen Fall, dass die untere Grenze des Arrays gleich Int32.MinValue und value nicht gefunden wird, gibt diese Methode Int32.MaxValue zurück, das System.Int32.MinValue - 1 ist.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.

Bei dieser Methode handelt es sich um eine O (n)-Operation, bei der n die Anzahl der Elemente vom Anfang array bis startIndex ist.This method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

In der .NET Framework-Version 2,0 verwendet diese Methode die Methoden Equals und CompareTo der Array, um zu bestimmen, ob das durch den value-Parameter angegebene Object vorhanden ist.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. In früheren Versionen der .NET Framework wurde diese Bestimmung mithilfe der Methoden Equals und CompareTo der valueObject selbst hergestellt.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

Siehe auch

LastIndexOf(Array, Object, Int32, Int32)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im eindimensionalen Array zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.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

Parameter

array
Array

Das zu durchsuchende eindimensionale Array.The one-dimensional Array to search.

value
Object

Das in array zu suchende Objekt.The object to locate in array.

startIndex
Int32

Der Startindex für die Rückwärtssuche.The starting index of the backward search.

count
Int32

Die Anzahl der Elemente im zu durchsuchenden Abschnitt.The number of elements in the section to search.

Gibt zurück

Der Index des letzten Vorkommens von value im Bereich von Elementen in array, das die durch count angegebene Anzahl von Elementen enthält und sich bis zum startIndex erstreckt, sofern gefunden, andernfalls die untere Grenze des Arrays 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.

Ausnahmen

array ist null.array is null.

startIndex liegt außerhalb des Bereichs der gültigen Indizes für array.startIndex is outside the range of valid indexes for array.

- oder --or- count ist kleiner als Null.count is less than zero.

- oder --or- startIndex und count geben keinen gültigen Abschnitt in array an.startIndex and count do not specify a valid section in array.

array ist mehrdimensional.array is multidimensional.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie der Index des letzten Vorkommens eines angegebenen Elements in einem Array bestimmt wird.The following code example shows how to determine the index of the last occurrence of a specified element in an array. Beachten Sie, dass die LastIndexOf-Methode eine Rückwärtssuche ist. Daher muss count kleiner als oder gleich (startIndex abzüglich der unteren Grenze des Arrays Plus 1) sein.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. 

Hinweise

Der eindimensionale Array wird rückwärts durchsucht, beginnend bei startIndex und endet bei startIndex minus count Plus 1, wenn count größer als 0 (null) ist.The one-dimensional Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

Die Elemente werden mit dem angegebenen Wert mit der Object.Equals-Methode verglichen.The elements are compared to the specified value using the Object.Equals method. Wenn der Elementtyp ein nicht System interner (benutzerdefinierter) Typ ist, wird dieEquals Implementierung dieses Typs verwendet.If the element type is a nonintrinsic (user-defined) type, theEquals implementation of that type is used.

Da die meisten Arrays eine untere Grenze von 0 (null) aufweisen, gibt diese Methode in der Regel-1 zurück, wenn value nicht gefunden wird.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. In dem seltenen Fall, dass die untere Grenze des Arrays gleich Int32.MinValue und value nicht gefunden wird, gibt diese Methode Int32.MaxValue zurück, das System.Int32.MinValue - 1 ist.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.

Bei dieser Methode handelt es sich um einen O (n)-Vorgang, bei dem n count ist.This method is an O(n) operation, where n is count.

In der .NET Framework-Version 2,0 verwendet diese Methode die Methoden Equals und CompareTo der Array, um zu bestimmen, ob das durch den value-Parameter angegebene Object vorhanden ist.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. In früheren Versionen der .NET Framework wurde diese Bestimmung mithilfe der Methoden Equals und CompareTo der valueObject selbst hergestellt.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

Siehe auch

LastIndexOf<T>(T[], T)

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des gesamten Array zurück.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

Typparameter

T

Der Typ der Elemente des Arrays.The type of the elements of the array.

Parameter

array
T[]

Das zu durchsuchende eindimensionale und nullbasierte Array.The one-dimensional, zero-based Array to search.

value
T

Das in array zu suchende Objekt.The object to locate in array.

Gibt zurück

Der nullbasierte Index des letzten Vorkommens von value im gesamten array, sofern gefunden; andernfalls –1.The zero-based index of the last occurrence of value within the entire array, if found; otherwise, -1.

Ausnahmen

array ist null.array is null.

Beispiele

Im folgenden Codebeispiel werden alle drei generischen über Ladungen der LastIndexOf-Methode veranschaulicht.The following code example demonstrates all three generic overloads of the LastIndexOf method. Ein Array von Zeichen folgen wird erstellt, wobei ein Eintrag zweimal an Indexposition 0 und Index Speicherort 5 angezeigt wird.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Die LastIndexOf<T>(T[], T)-Methoden Überladung durchsucht das gesamte Array vom Ende und findet das zweite Vorkommen der Zeichenfolge.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Die LastIndexOf<T>(T[], T, Int32)-Methoden Überladung wird verwendet, um das Array rückwärts zu durchsuchen, beginnend mit Indexposition 3 und bis zum Anfang des Arrays fortzufahren, und findet das erste Vorkommen der Zeichenfolge.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. Zum Schluss wird die LastIndexOf<T>(T[], T, Int32, Int32)-Methoden Überladung verwendet, um einen Bereich von vier Einträgen zu durchsuchen, beginnend bei Indexposition 4 und rückwärts, d. h., Sie durchsucht die Elemente an den Positionen 4, 3, 2 und 1. Diese Suche gibt-1 zurück, da in diesem Bereich keine Instanzen der Such Zeichenfolge vorhanden sind.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

Hinweise

Der Array wird rückwärts durchsucht, beginnend beim letzten Element und endet beim ersten Element.The Array is searched backward starting at the last element and ending at the first element.

Die Elemente werden mit dem angegebenen Wert mit der Object.Equals-Methode verglichen.The elements are compared to the specified value using the Object.Equals method. Wenn der Elementtyp ein nicht System interner (benutzerdefinierter) Typ ist, wird die Equals-Implementierung dieses Typs verwendet.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Bei dieser Methode handelt es sich um einen O (n)-Vorgang, bei dem n der Length von array ist.This method is an O(n) operation, where n is the Length of array.

Siehe auch

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

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im Array zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.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

Typparameter

T

Der Typ der Elemente des Arrays.The type of the elements of the array.

Parameter

array
T[]

Das zu durchsuchende eindimensionale und nullbasierte Array.The one-dimensional, zero-based Array to search.

value
T

Das in array zu suchende Objekt.The object to locate in array.

startIndex
Int32

Der nullbasierte Startindex für die Rückwärtssuche.The zero-based starting index of the backward search.

Gibt zurück

Der nullbasierte Index des letzten Vorkommens von value innerhalb des Bereichs von Elementen im array, das sich vom ersten Element bis startIndex erstreckt, sofern gefunden; andernfalls –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.

Ausnahmen

array ist null.array is null.

startIndex liegt außerhalb des Bereichs der gültigen Indizes für array.startIndex is outside the range of valid indexes for array.

Beispiele

Im folgenden Codebeispiel werden alle drei generischen über Ladungen der LastIndexOf-Methode veranschaulicht.The following code example demonstrates all three generic overloads of the LastIndexOf method. Ein Array von Zeichen folgen wird erstellt, wobei ein Eintrag zweimal an Indexposition 0 und Index Speicherort 5 angezeigt wird.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Die LastIndexOf<T>(T[], T)-Methoden Überladung durchsucht das gesamte Array vom Ende und findet das zweite Vorkommen der Zeichenfolge.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Die LastIndexOf<T>(T[], T, Int32)-Methoden Überladung wird verwendet, um das Array rückwärts zu durchsuchen, beginnend mit Indexposition 3 und bis zum Anfang des Arrays fortzufahren, und findet das erste Vorkommen der Zeichenfolge.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. Zum Schluss wird die LastIndexOf<T>(T[], T, Int32, Int32)-Methoden Überladung verwendet, um einen Bereich von vier Einträgen zu durchsuchen, beginnend bei Indexposition 4 und rückwärts, d. h., Sie durchsucht die Elemente an den Positionen 4, 3, 2 und 1. Diese Suche gibt-1 zurück, da in diesem Bereich keine Instanzen der Such Zeichenfolge vorhanden sind.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

Hinweise

Der Array wird rückwärts durchsucht, beginnend bei startIndex und endet beim ersten Element.The Array is searched backward starting at startIndex and ending at the first element.

Die Elemente werden mit dem angegebenen Wert mit der Object.Equals-Methode verglichen.The elements are compared to the specified value using the Object.Equals method. Wenn der Elementtyp ein nicht System interner (benutzerdefinierter) Typ ist, wird die Equals-Implementierung dieses Typs verwendet.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Bei dieser Methode handelt es sich um eine O (n)-Operation, bei der n die Anzahl der Elemente vom Anfang array bis startIndex ist.This method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

Siehe auch

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

Sucht nach dem angegebenen Objekt und gibt den Index des letzten Vorkommens innerhalb des Bereichs von Elementen im Array zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.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

Typparameter

T

Der Typ der Elemente des Arrays.The type of the elements of the array.

Parameter

array
T[]

Das zu durchsuchende eindimensionale und nullbasierte Array.The one-dimensional, zero-based Array to search.

value
T

Das in array zu suchende Objekt.The object to locate in array.

startIndex
Int32

Der nullbasierte Startindex für die Rückwärtssuche.The zero-based starting index of the backward search.

count
Int32

Die Anzahl der Elemente im zu durchsuchenden Abschnitt.The number of elements in the section to search.

Gibt zurück

Der nullbasierte Index des letzten Vorkommens von value innerhalb des Bereichs von Elementen im array, das die in count angegebene Anzahl von Elementen enthält und am startIndex endet, sofern gefunden; andernfalls –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.

Ausnahmen

array ist null.array is null.

startIndex liegt außerhalb des Bereichs der gültigen Indizes für array.startIndex is outside the range of valid indexes for array.

- oder --or- count ist kleiner als Null.count is less than zero.

- oder --or- startIndex und count geben keinen gültigen Abschnitt im array an.startIndex and count do not specify a valid section in array.

Beispiele

Im folgenden Codebeispiel werden alle drei generischen über Ladungen der LastIndexOf-Methode veranschaulicht.The following code example demonstrates all three generic overloads of the LastIndexOf method. Ein Array von Zeichen folgen wird erstellt, wobei ein Eintrag zweimal an Indexposition 0 und Index Speicherort 5 angezeigt wird.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Die LastIndexOf<T>(T[], T)-Methoden Überladung durchsucht das gesamte Array vom Ende und findet das zweite Vorkommen der Zeichenfolge.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Die LastIndexOf<T>(T[], T, Int32)-Methoden Überladung wird verwendet, um das Array rückwärts zu durchsuchen, beginnend mit Indexposition 3 und bis zum Anfang des Arrays fortzufahren, und findet das erste Vorkommen der Zeichenfolge.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. Zum Schluss wird die LastIndexOf<T>(T[], T, Int32, Int32)-Methoden Überladung verwendet, um einen Bereich von vier Einträgen zu durchsuchen, beginnend bei Indexposition 4 und rückwärts, d. h., Sie durchsucht die Elemente an den Positionen 4, 3, 2 und 1. Diese Suche gibt-1 zurück, da in diesem Bereich keine Instanzen der Such Zeichenfolge vorhanden sind.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

Hinweise

Der Array wird rückwärts durchsucht, beginnend bei startIndex und endet bei startIndex minus count Plus 1, wenn count größer als 0 (null) ist.The Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

Die Elemente werden mit dem angegebenen Wert mit der Object.Equals-Methode verglichen.The elements are compared to the specified value using the Object.Equals method. Wenn der Elementtyp ein nicht System interner (benutzerdefinierter) Typ ist, wird die Equals-Implementierung dieses Typs verwendet.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Bei dieser Methode handelt es sich um einen O (n)-Vorgang, bei dem n count ist.This method is an O(n) operation, where n is count.

Siehe auch

Gilt für: