Array.LastIndexOf Method

Definition

Vrátí index posledního výskytu hodnoty v jednorozměrném Array nebo v části Array.Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.

Overloads

LastIndexOf(Array, Object)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého jednorozměrného Array.Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

LastIndexOf(Array, Object, Int32)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků v jednorozměrném Array, který rozšiřuje od prvního prvku na zadaný index.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)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků v jednorozměrném Array, který obsahuje zadaný počet prvků a končí v zadaném indexu.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)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého Array.Searches for the specified object and returns the index of the last occurrence within the entire Array.

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

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků v Array, který rozšiřuje z prvního prvku na zadaný index.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)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků Array, který obsahuje zadaný počet prvků a končí v zadaném indexu.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)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého jednorozměrného Array.Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

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

Parameters

array
Array

Jednorozměrné Array pro hledání.The one-dimensional Array to search.

value
Object

Objekt, který se má najít v array.The object to locate in array.

Returns

Int32

Index posledního výskytu value v rámci celého array, pokud se našel; jinak spodní mez pole 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.

Exceptions

array je null.array is null.

Pole array je multidimenzionální.array is multidimensional.

Examples

Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku v poli.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.
 */
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(String), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

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

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

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

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

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

/*
This code produces the following output.

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

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

Remarks

Jednorozměrné Array se prohledává zpětně počínaje posledním prvkem a končí prvním prvkem.The one-dimensional Array is searched backward starting at the last element and ending at the first element.

Prvky jsou porovnány se zadanou hodnotou pomocí metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Pokud typ elementu je nevnitřní (uživatelsky definovaný) typ, je použita Equals implementace tohoto typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Vzhledem k tomu, že většina polí bude mít dolní mez nula, tato metoda obecně vrátí-1, když value nenajde.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. V neobvyklém případě, že dolní mez pole je rovna Int32.MinValue a value nebyla nalezena, vrátí tato metoda Int32.MaxValue, která je 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.

Tato metoda je operace O (n), kde n je Length array.This method is an O(n) operation, where n is the Length of array.

V .NET Framework verze 2,0 Tato metoda používá metody Equals a CompareTo Array k určení, zda Object určené parametrem value existuje.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. V dřívějších verzích .NET Framework bylo toto určení provedeno pomocí metod Equals a CompareTo valuesamotného Object.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 parametru item u objektů v kolekci.CompareTo methods of the item parameter on the objects in the collection.

See also

LastIndexOf(Array, Object, Int32)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků v jednorozměrném Array, který rozšiřuje od prvního prvku na zadaný index.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
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer) As Integer

Parameters

array
Array

Jednorozměrné Array pro hledání.The one-dimensional Array to search.

value
Object

Objekt, který se má najít v array.The object to locate in array.

startIndex
Int32

Počáteční index zpětného vyhledávání.The starting index of the backward search.

Returns

Int32

Index posledního výskytu value v rámci rozsahu prvků v array, který rozšiřuje z prvního prvku na startIndex, pokud byl nalezen; jinak spodní mez pole 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.

Exceptions

array je null.array is null.

startIndex je mimo rozsah platných indexů pro array.startIndex is outside the range of valid indexes for array.

Pole array je multidimenzionální.array is multidimensional.

Examples

Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku v poli.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.
 */
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(String), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

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

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

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

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

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

/*
This code produces the following output.

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

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

Remarks

Jednorozměrné Array se prohledává zpětně počínaje startIndex a končí prvním prvkem.The one-dimensional Array is searched backward starting at startIndex and ending at the first element.

Prvky jsou porovnány se zadanou hodnotou pomocí metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Pokud typ elementu je nevnitřní (uživatelsky definovaný) typ, je použita Equals implementace tohoto typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Vzhledem k tomu, že většina polí bude mít dolní mez nula, tato metoda obecně vrátí-1, když value nenajde.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. V neobvyklém případě, že dolní mez pole je rovna Int32.MinValue a value nebyla nalezena, vrátí tato metoda Int32.MaxValue, která je 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.

Tato metoda je operace O (n), kde n je počet prvků od začá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.

V .NET Framework verze 2,0 Tato metoda používá metody Equals a CompareTo Array k určení, zda Object určené parametrem value existuje.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. V dřívějších verzích .NET Framework bylo toto určení provedeno pomocí metod Equals a CompareTo valuesamotného Object.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

See also

LastIndexOf(Array, Object, Int32, Int32)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků v jednorozměrném Array, který obsahuje zadaný počet prvků a končí v zadaném indexu.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
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer

Parameters

array
Array

Jednorozměrné Array pro hledání.The one-dimensional Array to search.

value
Object

Objekt, který se má najít v array.The object to locate in array.

startIndex
Int32

Počáteční index zpětného vyhledávání.The starting index of the backward search.

count
Int32

Počet prvků v prohledávané části.The number of elements in the section to search.

Returns

Int32

Index posledního výskytu value v rámci rozsahu prvků v array, který obsahuje počet prvků zadaný v count a končí v startIndex, pokud se najde; jinak spodní mez pole 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.

Exceptions

array je null.array is null.

startIndex je mimo rozsah platných indexů pro array.startIndex is outside the range of valid indexes for array.

-nebo--or- Hodnota count je menší než nula.count is less than zero.

-nebo--or- startIndex a count nespecifikují v arrayplatný oddíl.startIndex and count do not specify a valid section in array.

Pole array je multidimenzionální.array is multidimensional.

Examples

Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku v poli.The following code example shows how to determine the index of the last occurrence of a specified element in an array. Všimněte si, že metoda LastIndexOf je zpětné vyhledávání; Proto count musí být menší než nebo rovno (startIndex mínus spodní mez pole 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.
 */
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(String), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

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

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

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

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

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

/*
This code produces the following output.

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

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

Remarks

Jednorozměrné Array se prohledává zpětně počínaje startIndex a končí na startIndex mínus count plus 1, pokud je count větší než 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.

Prvky jsou porovnány se zadanou hodnotou pomocí metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Pokud typ elementu je nevnitřní (uživatelsky definovaný) typ, je použitaEquals implementace tohoto typu.If the element type is a nonintrinsic (user-defined) type, theEquals implementation of that type is used.

Vzhledem k tomu, že většina polí bude mít dolní mez nula, tato metoda obecně vrátí-1, když value nenajde.Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. V neobvyklém případě, že dolní mez pole je rovna Int32.MinValue a value nebyla nalezena, vrátí tato metoda Int32.MaxValue, která je 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.

Tato metoda je operace O (n), kde n je count.This method is an O(n) operation, where n is count.

V .NET Framework verze 2,0 Tato metoda používá metody Equals a CompareTo Array k určení, zda Object určené parametrem value existuje.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. V dřívějších verzích .NET Framework bylo toto určení provedeno pomocí metod Equals a CompareTo valuesamotného Object.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the valueObject itself.

See also

LastIndexOf<T>(T[], T)

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého 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

Type Parameters

T

Typ prvků pole.The type of the elements of the array.

Parameters

array
T[]

Jednorozměrné Array založené na nule pro hledání.The one-dimensional, zero-based Array to search.

value
T

Objekt, který se má najít v array.The object to locate in array.

Returns

Int32

Index založený na nule posledního výskytu value v rámci celého array, pokud byl nalezen; v opačném případě-1.The zero-based index of the last occurrence of value within the entire array, if found; otherwise, -1.

Exceptions

array je null.array is null.

Examples

Následující příklad kódu ukazuje všechna tři obecná přetížení metody LastIndexOf.The following code example demonstrates all three generic overloads of the LastIndexOf method. Je vytvořeno pole řetězců, v němž se jedna položka vyskytuje dvakrát, jednou s indexem 0 a podruhé s indexem 5.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Přetížení metody LastIndexOf<T>(T[], T) vyhledá celé pole od konce a nalezne druhý výskyt řetězce.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Přetížení metody LastIndexOf<T>(T[], T, Int32) slouží k vyhledávání v poli směrem dozadu od indexu s umístěním 3 a pokračuje na začátek pole a vyhledá první výskyt řetězce.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. Nakonec se pomocí přetížení metody LastIndexOf<T>(T[], T, Int32, Int32) vyhledá rozsah čtyř položek, počínaje indexovým umístěním 4 a rozšířením zpět (to znamená, že prohledává položky v umístěních 4, 3, 2 a 1). Toto hledání vrátí hodnotu-1, protože v tomto rozsahu nejsou žádné instance hledaného řetězce.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
 */
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

Remarks

Array se prohledává zpětně počínaje posledním prvkem a končí prvním prvkem.The Array is searched backward starting at the last element and ending at the first element.

Prvky jsou porovnány se zadanou hodnotou pomocí metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Pokud typ elementu je nevnitřní (uživatelsky definovaný) typ, je použita Equals implementace tohoto typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Tato metoda je operace O (n), kde n je Length array.This method is an O(n) operation, where n is the Length of array.

See also

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

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků v Array, který rozšiřuje z prvního prvku na zadaný index.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

Type Parameters

T

Typ prvků pole.The type of the elements of the array.

Parameters

array
T[]

Jednorozměrné Array založené na nule pro hledání.The one-dimensional, zero-based Array to search.

value
T

Objekt, který se má najít v array.The object to locate in array.

startIndex
Int32

Počáteční index zpětného vyhledávání začínající od nuly.The zero-based starting index of the backward search.

Returns

Int32

Index založený na nule posledního výskytu value v rámci rozsahu prvků v array, který rozšiřuje z prvního prvku na startIndex, pokud byl nalezen; v opačném případě-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.

Exceptions

array je null.array is null.

startIndex je mimo rozsah platných indexů pro array.startIndex is outside the range of valid indexes for array.

Examples

Následující příklad kódu ukazuje všechna tři obecná přetížení metody LastIndexOf.The following code example demonstrates all three generic overloads of the LastIndexOf method. Je vytvořeno pole řetězců, v němž se jedna položka vyskytuje dvakrát, jednou s indexem 0 a podruhé s indexem 5.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Přetížení metody LastIndexOf<T>(T[], T) vyhledá celé pole od konce a nalezne druhý výskyt řetězce.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Přetížení metody LastIndexOf<T>(T[], T, Int32) slouží k vyhledávání v poli směrem dozadu od indexu s umístěním 3 a pokračuje na začátek pole a vyhledá první výskyt řetězce.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. Nakonec se pomocí přetížení metody LastIndexOf<T>(T[], T, Int32, Int32) vyhledá rozsah čtyř položek, počínaje indexovým umístěním 4 a rozšířením zpět (to znamená, že prohledává položky v umístěních 4, 3, 2 a 1). Toto hledání vrátí hodnotu-1, protože v tomto rozsahu nejsou žádné instance hledaného řetězce.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
 */
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

Remarks

Array je prohledáván zpětně od startIndex a končí prvním prvkem.The Array is searched backward starting at startIndex and ending at the first element.

Prvky jsou porovnány se zadanou hodnotou pomocí metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Pokud typ elementu je nevnitřní (uživatelsky definovaný) typ, je použita Equals implementace tohoto typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Tato metoda je operace O (n), kde n je počet prvků od začá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.

See also

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

Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci rozsahu prvků Array, který obsahuje zadaný počet prvků a končí v zadaném indexu.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

Type Parameters

T

Typ prvků pole.The type of the elements of the array.

Parameters

array
T[]

Jednorozměrné Array založené na nule pro hledání.The one-dimensional, zero-based Array to search.

value
T

Objekt, který se má najít v array.The object to locate in array.

startIndex
Int32

Počáteční index zpětného vyhledávání začínající od nuly.The zero-based starting index of the backward search.

count
Int32

Počet prvků v prohledávané části.The number of elements in the section to search.

Returns

Int32

Index založený na nule posledního výskytu value v rámci rozsahu prvků v array, který obsahuje počet prvků určený v count a končí na startIndex, pokud je nalezen; v opačném případě-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.

Exceptions

array je null.array is null.

startIndex je mimo rozsah platných indexů pro array.startIndex is outside the range of valid indexes for array.

-nebo--or- Hodnota count je menší než nula.count is less than zero.

-nebo--or- startIndex a count nespecifikují v arrayplatný oddíl.startIndex and count do not specify a valid section in array.

Examples

Následující příklad kódu ukazuje všechna tři obecná přetížení metody LastIndexOf.The following code example demonstrates all three generic overloads of the LastIndexOf method. Je vytvořeno pole řetězců, v němž se jedna položka vyskytuje dvakrát, jednou s indexem 0 a podruhé s indexem 5.An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. Přetížení metody LastIndexOf<T>(T[], T) vyhledá celé pole od konce a nalezne druhý výskyt řetězce.The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. Přetížení metody LastIndexOf<T>(T[], T, Int32) slouží k vyhledávání v poli směrem dozadu od indexu s umístěním 3 a pokračuje na začátek pole a vyhledá první výskyt řetězce.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. Nakonec se pomocí přetížení metody LastIndexOf<T>(T[], T, Int32, Int32) vyhledá rozsah čtyř položek, počínaje indexovým umístěním 4 a rozšířením zpět (to znamená, že prohledává položky v umístěních 4, 3, 2 a 1). Toto hledání vrátí hodnotu-1, protože v tomto rozsahu nejsou žádné instance hledaného řetězce.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
 */
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

Remarks

Array se prohledává zpětně počínaje startIndex a končí na startIndex mínus count plus 1, pokud je count větší než 0.The Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

Prvky jsou porovnány se zadanou hodnotou pomocí metody Object.Equals.The elements are compared to the specified value using the Object.Equals method. Pokud typ elementu je nevnitřní (uživatelsky definovaný) typ, je použita Equals implementace tohoto typu.If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Tato metoda je operace O (n), kde n je count.This method is an O(n) operation, where n is count.

See also

Applies to