Array.LastIndexOf Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Surcharges
| Nom | Description |
|---|---|
| LastIndexOf(Array, Object) |
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans l’ensemble unidimensionnel Array. |
| LastIndexOf(Array, Object, Int32) |
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments dans la dimension qui Array s’étend du premier élément à l’index spécifié. |
| LastIndexOf(Array, Object, Int32, Int32) |
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments dans la dimension Array qui contient le nombre spécifié d’éléments et se termine à l’index spécifié. |
| LastIndexOf<T>(T[], T) |
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans l’ensemble Array. |
| LastIndexOf<T>(T[], T, Int32) |
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments de l’élément Array qui s’étend du premier élément à l’index spécifié. |
| LastIndexOf<T>(T[], T, Int32, Int32) |
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments dans laquelle Array contient le nombre spécifié d’éléments et se termine à l’index spécifié. |
LastIndexOf(Array, Object)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans l’ensemble unidimensionnel Array.
public:
static int LastIndexOf(Array ^ array, System::Object ^ value);
public static int LastIndexOf(Array array, object value);
public static int LastIndexOf(Array array, object? value);
static member LastIndexOf : Array * obj -> int
Public Shared Function LastIndexOf (array As Array, value As Object) As Integer
Paramètres
- value
- Object
Objet à localiser dans array.
Retours
Index de la dernière occurrence de value l’intégralité array, s’il est trouvé ; sinon, la limite inférieure du tableau moins 1.
Exceptions
array a la valeur null.
array est multidimensionnel.
Exemples
L’exemple de code suivant montre comment déterminer l’index de la dernière occurrence d’un élément spécifié dans un tableau.
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// This code produces the following output.
//
// The Array contains the following values:
// [0]: the
// [1]: quick
// [2]: brown
// [3]: fox
// [4]: jumps
// [5]: over
// [6]: the
// [7]: lazy
// [8]: dog
// [9]: in
// [10]: the
// [11]: barn
// The last occurrence of "the" is at index 10.
// The last occurrence of "the" between the start and index 8 is at index 6.
// The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}
/*
This code produces the following output.
The Array contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
[9]: in
[10]: the
[11]: barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first
' section of the Array.
myIndex = Array.LastIndexOf(myArray, myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start " _
+ "and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section
' of the Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces the following output.
'
' The Array contains the following values:
' [0]: the
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
' [9]: in
' [10]: the
' [11]: barn
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.
Remarques
L’élément unidimensionnel Array est recherché vers l’arrière à partir du dernier élément et se termine au premier élément.
Les éléments sont comparés à la valeur spécifiée à l’aide de la Object.Equals méthode. Si le type d’élément est un type non-trinsique (défini par l’utilisateur), l’implémentation Equals de ce type est utilisée.
Étant donné que la plupart des tableaux auront une limite inférieure de zéro, cette méthode retourne généralement -1 quand value elle est introuvable. Dans les rares cas où la limite inférieure du tableau est égale et Int32.MinValuevalue introuvable, cette méthode retourne Int32.MaxValue, qui est System.Int32.MinValue - 1.
Cette méthode est une opération O(n), où n est le Lengtharray.
Cette méthode utilise les méthodes et CompareTo les Equals méthodes de la Array méthode pour déterminer si le Objectvalue paramètre spécifié existe.
CompareTo méthodes du item paramètre sur les objets de la collection.
Voir aussi
S’applique à
LastIndexOf(Array, Object, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments dans la dimension qui Array s’étend du premier élément à l’index spécifié.
public:
static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int LastIndexOf(Array array, object value, int startIndex);
public static int LastIndexOf(Array array, object? value, int startIndex);
static member LastIndexOf : Array * obj * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer) As Integer
Paramètres
- value
- Object
Objet à localiser dans array.
- startIndex
- Int32
Index de départ de la recherche descendante.
Retours
Index de la dernière occurrence de value la plage d’éléments dans array laquelle s’étend le premier élément à startIndex, s’il est trouvé ; sinon, la limite inférieure du tableau moins 1.
Exceptions
array a la valeur null.
startIndex est en dehors de la plage d’index valides pour array.
array est multidimensionnel.
Exemples
L’exemple de code suivant montre comment déterminer l’index de la dernière occurrence d’un élément spécifié dans un tableau.
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// This code produces the following output.
//
// The Array contains the following values:
// [0]: the
// [1]: quick
// [2]: brown
// [3]: fox
// [4]: jumps
// [5]: over
// [6]: the
// [7]: lazy
// [8]: dog
// [9]: in
// [10]: the
// [11]: barn
// The last occurrence of "the" is at index 10.
// The last occurrence of "the" between the start and index 8 is at index 6.
// The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}
/*
This code produces the following output.
The Array contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
[9]: in
[10]: the
[11]: barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first
' section of the Array.
myIndex = Array.LastIndexOf(myArray, myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start " _
+ "and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section
' of the Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces the following output.
'
' The Array contains the following values:
' [0]: the
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
' [9]: in
' [10]: the
' [11]: barn
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.
Remarques
L’élément unidimensionnel Array est recherché à l’arrière à partir et startIndex se termine au premier élément.
Les éléments sont comparés à la valeur spécifiée à l’aide de la Object.Equals méthode. Si le type d’élément est un type non-trinsique (défini par l’utilisateur), l’implémentation Equals de ce type est utilisée.
Étant donné que la plupart des tableaux auront une limite inférieure de zéro, cette méthode retourne généralement -1 quand value elle est introuvable. Dans les rares cas où la limite inférieure du tableau est égale et Int32.MinValuevalue introuvable, cette méthode retourne Int32.MaxValue, qui est System.Int32.MinValue - 1.
Cette méthode est une opération O(n), où n est le nombre d’éléments du début array à startIndex.
Cette méthode utilise les méthodes et CompareTo les Equals méthodes de la Array méthode pour déterminer si le Objectvalue paramètre spécifié existe.
Voir aussi
S’applique à
LastIndexOf(Array, Object, Int32, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments dans la dimension Array qui contient le nombre spécifié d’éléments et se termine à l’index spécifié.
public:
static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int LastIndexOf(Array array, object value, int startIndex, int count);
public static int LastIndexOf(Array array, object? value, int startIndex, int count);
static member LastIndexOf : Array * obj * int * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer
Paramètres
- value
- Object
Objet à localiser dans array.
- startIndex
- Int32
Index de départ de la recherche descendante.
- count
- Int32
Nombre d’éléments de la section à rechercher.
Retours
Index de la dernière occurrence de value la plage d’éléments dans array laquelle contient le nombre d’éléments spécifiés et count se termine par startIndex, s’il est trouvé ; sinon, la limite inférieure du tableau moins 1.
Exceptions
array a la valeur null.
startIndex est en dehors de la plage d’index valides pour array.
-ou-
count est inférieur à zéro.
-ou-
startIndex et count ne spécifiez pas de section valide dans array.
array est multidimensionnel.
Exemples
L’exemple de code suivant montre comment déterminer l’index de la dernière occurrence d’un élément spécifié dans un tableau. Notez que la LastIndexOf méthode est une recherche descendante ; par conséquent, count doit être inférieure ou égale à (startIndex moins la limite inférieure du tableau plus 1).
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// This code produces the following output.
//
// The Array contains the following values:
// [0]: the
// [1]: quick
// [2]: brown
// [3]: fox
// [4]: jumps
// [5]: over
// [6]: the
// [7]: lazy
// [8]: dog
// [9]: in
// [10]: the
// [11]: barn
// The last occurrence of "the" is at index 10.
// The last occurrence of "the" between the start and index 8 is at index 6.
// The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}
/*
This code produces the following output.
The Array contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
[9]: in
[10]: the
[11]: barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first
' section of the Array.
myIndex = Array.LastIndexOf(myArray, myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start " _
+ "and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section
' of the Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces the following output.
'
' The Array contains the following values:
' [0]: the
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
' [9]: in
' [10]: the
' [11]: barn
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.
Remarques
La recherche en une dimension Array est effectuée à l’arrière à startIndex partir et se termine à startIndex moins count 1, si count elle est supérieure à 0.
Les éléments sont comparés à la valeur spécifiée à l’aide de la Object.Equals méthode. Si le type d’élément est un type non-trinsique (défini par l’utilisateur), l’implémentationEquals de ce type est utilisée.
Étant donné que la plupart des tableaux auront une limite inférieure de zéro, cette méthode retourne généralement -1 quand value elle est introuvable. Dans les rares cas où la limite inférieure du tableau est égale et Int32.MinValuevalue introuvable, cette méthode retourne Int32.MaxValue, qui est System.Int32.MinValue - 1.
Cette méthode est une opération O(n), où n est count.
Cette méthode utilise les méthodes et CompareTo les Equals méthodes de la Array méthode pour déterminer si le Objectvalue paramètre spécifié existe.
Voir aussi
S’applique à
LastIndexOf<T>(T[], T)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans l’ensemble 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
Paramètres de type
- T
Type des éléments du tableau.
Paramètres
- array
- T[]
Basé sur Array zéro unidimensionnel à rechercher.
- value
- T
Objet à localiser dans array.
Retours
Index de base zéro de la dernière occurrence de value l’ensemble array, s’il est trouvé ; sinon, -1.
Exceptions
array a la valeur null.
Exemples
L’exemple de code suivant illustre les trois surcharges génériques de la LastIndexOf méthode. Un tableau de chaînes est créé, avec une entrée qui apparaît deux fois, à l’emplacement d’index 0 et à l’emplacement d’index 5. La LastIndexOf<T>(T[], T) surcharge de méthode recherche l’intégralité du tableau à partir de la fin et recherche la deuxième occurrence de la chaîne. La LastIndexOf<T>(T[], T, Int32) surcharge de méthode est utilisée pour rechercher le tableau vers l’arrière à partir de l’emplacement d’index 3 et passer au début du tableau et recherche la première occurrence de la chaîne. Enfin, la LastIndexOf<T>(T[], T, Int32, Int32) surcharge de méthode est utilisée pour rechercher une plage de quatre entrées, en commençant à l’emplacement d’index 4 et en s’étendant vers l’arrière (autrement dit, il recherche les éléments aux emplacements 4, 3, 2 et 1) ; cette recherche retourne -1, car il n’existe aucune instance de la chaîne de recherche dans cette plage.
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Remarques
La Array recherche est effectuée vers l’arrière à partir du dernier élément et se termine au premier élément.
Les éléments sont comparés à la valeur spécifiée à l’aide de la Object.Equals méthode. Si le type d’élément est un type non-trinsique (défini par l’utilisateur), l’implémentation Equals de ce type est utilisée.
Cette méthode est une opération O(n), où n est le Lengtharray.
Voir aussi
S’applique à
LastIndexOf<T>(T[], T, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments de l’élément Array qui s’étend du premier élément à l’index spécifié.
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
Paramètres de type
- T
Type des éléments du tableau.
Paramètres
- array
- T[]
Basé sur Array zéro unidimensionnel à rechercher.
- value
- T
Objet à localiser dans array.
- startIndex
- Int32
Index de départ de base zéro de la recherche descendante.
Retours
Index de base zéro de la dernière occurrence de value la plage d’éléments dans array laquelle s’étend le premier élément à startIndex, s’il est trouvé ; sinon, -1.
Exceptions
array a la valeur null.
startIndex est en dehors de la plage d’index valides pour array.
Exemples
L’exemple de code suivant illustre les trois surcharges génériques de la LastIndexOf méthode. Un tableau de chaînes est créé, avec une entrée qui apparaît deux fois, à l’emplacement d’index 0 et à l’emplacement d’index 5. La LastIndexOf<T>(T[], T) surcharge de méthode recherche l’intégralité du tableau à partir de la fin et recherche la deuxième occurrence de la chaîne. La LastIndexOf<T>(T[], T, Int32) surcharge de méthode est utilisée pour rechercher le tableau vers l’arrière à partir de l’emplacement d’index 3 et passer au début du tableau et recherche la première occurrence de la chaîne. Enfin, la LastIndexOf<T>(T[], T, Int32, Int32) surcharge de méthode est utilisée pour rechercher une plage de quatre entrées, en commençant à l’emplacement d’index 4 et en s’étendant vers l’arrière (autrement dit, il recherche les éléments aux emplacements 4, 3, 2 et 1) ; cette recherche retourne -1, car il n’existe aucune instance de la chaîne de recherche dans cette plage.
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Remarques
L’objet Array est recherché à l’arrière à startIndex partir et se termine au premier élément.
Les éléments sont comparés à la valeur spécifiée à l’aide de la Object.Equals méthode. Si le type d’élément est un type non-trinsique (défini par l’utilisateur), l’implémentation Equals de ce type est utilisée.
Cette méthode est une opération O(n), où n est le nombre d’éléments du début array à startIndex.
Voir aussi
S’applique à
LastIndexOf<T>(T[], T, Int32, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
Recherche l’objet spécifié et retourne l’index de la dernière occurrence dans la plage d’éléments dans laquelle Array contient le nombre spécifié d’éléments et se termine à l’index spécifié.
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
Paramètres de type
- T
Type des éléments du tableau.
Paramètres
- array
- T[]
Basé sur Array zéro unidimensionnel à rechercher.
- value
- T
Objet à localiser dans array.
- startIndex
- Int32
Index de départ de base zéro de la recherche descendante.
- count
- Int32
Nombre d’éléments de la section à rechercher.
Retours
Index de base zéro de la dernière occurrence de value la plage d’éléments dans array laquelle contient le nombre d’éléments spécifiés et count se termine à startIndex, s’il est trouvé ; sinon, -1.
Exceptions
array a la valeur null.
startIndex est en dehors de la plage d’index valides pour array.
-ou-
count est inférieur à zéro.
-ou-
startIndex et count ne spécifiez pas de section valide dans array.
Exemples
L’exemple de code suivant illustre les trois surcharges génériques de la LastIndexOf méthode. Un tableau de chaînes est créé, avec une entrée qui apparaît deux fois, à l’emplacement d’index 0 et à l’emplacement d’index 5. La LastIndexOf<T>(T[], T) surcharge de méthode recherche l’intégralité du tableau à partir de la fin et recherche la deuxième occurrence de la chaîne. La LastIndexOf<T>(T[], T, Int32) surcharge de méthode est utilisée pour rechercher le tableau vers l’arrière à partir de l’emplacement d’index 3 et passer au début du tableau et recherche la première occurrence de la chaîne. Enfin, la LastIndexOf<T>(T[], T, Int32, Int32) surcharge de méthode est utilisée pour rechercher une plage de quatre entrées, en commençant à l’emplacement d’index 4 et en s’étendant vers l’arrière (autrement dit, il recherche les éléments aux emplacements 4, 3, 2 et 1) ; cette recherche retourne -1, car il n’existe aucune instance de la chaîne de recherche dans cette plage.
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Remarques
La Array recherche est effectuée vers l’arrière à startIndex partir et se terminant à startIndex moins count 1, si count elle est supérieure à 0.
Les éléments sont comparés à la valeur spécifiée à l’aide de la Object.Equals méthode. Si le type d’élément est un type non-trinsique (défini par l’utilisateur), l’implémentation Equals de ce type est utilisée.
Cette méthode est une opération O(n), où n est count.