Array.LastIndexOf Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Overload
LastIndexOf(Array, Object) |
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'oggetto Array unidimensionale intero. |
LastIndexOf(Array, Object, Int32) |
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi dell'oggetto Array unidimensionale compreso tra il primo elemento e l'indice specificato. |
LastIndexOf(Array, Object, Int32, Int32) |
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi nell'oggetto Array unidimensionale che contiene il numero di elementi specificato e termina in corrispondenza dell'indice specificato. |
LastIndexOf<T>(T[], T) |
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intero oggetto Array. |
LastIndexOf<T>(T[], T, Int32) |
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi dell'oggetto Array compreso tra il primo elemento e l'indice specificato. |
LastIndexOf<T>(T[], T, Int32, Int32) |
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi dell'oggetto Array che contiene il numero specificato di elementi e termina in corrispondenza dell'indice specificato. |
LastIndexOf(Array, Object)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'oggetto Array unidimensionale intero.
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
Parametri
- value
- Object
Oggetto da trovare in array
.
Restituisce
Indice dell'ultima occorrenza di value
all'interno dell'intero oggetto array
, se trovato; in caso contrario, verrà restituito il limite inferiore della matrice meno 1.
Eccezioni
array
è null
.
array
è multidimensionale.
Esempio
Nell'esempio di codice seguente viene illustrato come determinare l'indice dell'ultima occorrenza di un elemento specificato in una matrice.
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.
*/
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.
Commenti
La ricerca unidimensionale Array viene eseguita all'indietro a partire dall'ultimo elemento e termina con il primo elemento.
Gli elementi vengono confrontati con il valore specificato usando il Object.Equals metodo . Se il tipo di elemento è un tipo non intrinseco (definito dall'utente), viene usata l'implementazione Equals
di tale tipo.
Poiché la maggior parte delle matrici avrà un limite inferiore pari a zero, questo metodo restituirà in genere -1 quando value
non viene trovato. Nel raro caso in cui il limite inferiore della matrice sia uguale a Int32.MinValue e value
non viene trovato, questo metodo restituisce Int32.MaxValue, ovvero System.Int32.MinValue - 1
.
Questo metodo è un'operazione O(n
), dove n
è l'oggetto Length di array
.
In .NET Framework 2.0 e versioni successive questo metodo usa i Equals metodi e CompareTo di Array per determinare se l'oggetto Objectvalue
specificato dal parametro esiste. Nelle versioni precedenti di .NET Framework, questa determinazione è stata effettuata usando i Equals metodi e CompareTo di value
Object se stesso.
CompareTo metodi del item
parametro sugli oggetti nell'insieme.
Vedi anche
- IndexOf
- Esecuzione di operazioni sulle stringhe indipendenti dalle impostazioni cultura nelle matrici
Si applica a
LastIndexOf(Array, Object, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi dell'oggetto Array unidimensionale compreso tra il primo elemento e l'indice specificato.
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
Parametri
- value
- Object
Oggetto da trovare in array
.
- startIndex
- Int32
Indice iniziale della ricerca all'indietro.
Restituisce
Indice della prima occorrenza del valore indicato nel parametro value
all'interno dell'intervallo di elementi della matrice array
compreso tra il primo elemento e l'indice startIndex
, se trovato; in caso contrario, verrà restituito il limite inferiore della matrice meno 1.
Eccezioni
array
è null
.
startIndex
non è compreso nell'intervallo di indici validi per array
.
array
è multidimensionale.
Esempio
Nell'esempio di codice seguente viene illustrato come determinare l'indice dell'ultima occorrenza di un elemento specificato in una matrice.
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.
*/
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.
Commenti
La ricerca unidimensionale Array viene eseguita all'indietro a partire da startIndex
e termina con il primo elemento.
Gli elementi vengono confrontati con il valore specificato usando il Object.Equals metodo . Se il tipo di elemento è un tipo non intrinseco (definito dall'utente), viene usata l'implementazione Equals
di tale tipo.
Poiché la maggior parte delle matrici avrà un limite inferiore pari a zero, questo metodo restituirà in genere -1 quando value
non viene trovato. Nel raro caso in cui il limite inferiore della matrice sia uguale a Int32.MinValue e value
non viene trovato, questo metodo restituisce Int32.MaxValue, ovvero System.Int32.MinValue - 1
.
Questo metodo è un'operazione O(n
), dove n
è il numero di elementi dall'inizio di array
a startIndex
.
In .NET Framework 2.0 e versioni successive questo metodo usa i Equals metodi e CompareTo di Array per determinare se l'oggetto Objectvalue
specificato dal parametro esiste. Nelle versioni precedenti di .NET Framework, questa determinazione è stata effettuata usando i Equals metodi e CompareTo di value
Object se stesso.
Vedi anche
- IndexOf
- Esecuzione di operazioni sulle stringhe indipendenti dalle impostazioni cultura nelle matrici
Si applica a
LastIndexOf(Array, Object, Int32, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi nell'oggetto Array unidimensionale che contiene il numero di elementi specificato e termina in corrispondenza dell'indice specificato.
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
Parametri
- value
- Object
Oggetto da trovare in array
.
- startIndex
- Int32
Indice iniziale della ricerca all'indietro.
- count
- Int32
Numero di elementi nella sezione in cui eseguire la ricerca.
Restituisce
Indice dell'ultima occorrenza dell'oggetto value
all'interno dell'intervallo di elementi in array
che contiene il numero di elementi specificato in count
e termina in corrispondenza di startIndex
, se trovato; in caso contrario, verrà restituito il limite inferiore della matrice meno 1.
Eccezioni
array
è null
.
startIndex
non è compreso nell'intervallo di indici validi per array
.
-oppure-
count
è minore di zero.
-oppure-
startIndex
e count
non specificano una sezione valida in array
.
array
è multidimensionale.
Esempio
Nell'esempio di codice seguente viene illustrato come determinare l'indice dell'ultima occorrenza di un elemento specificato in una matrice. Si noti che il LastIndexOf metodo è una ricerca all'indietro. Pertanto, count
deve essere minore o uguale a (startIndex
meno il limite inferiore della matrice più 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.
*/
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.
Commenti
La ricerca unidimensionale Array viene eseguita all'indietro a partire da startIndex
e termina con startIndex
meno count
più 1, se count
è maggiore di 0.
Gli elementi vengono confrontati con il valore specificato usando il Object.Equals metodo . Se il tipo di elemento è un tipo non intrinseco (definito dall'utente), viene usata l'implementazioneEquals
di tale tipo.
Poiché la maggior parte delle matrici avrà un limite inferiore pari a zero, questo metodo restituirà in genere -1 quando value
non viene trovato. Nel raro caso in cui il limite inferiore della matrice sia uguale a Int32.MinValue e value
non viene trovato, questo metodo restituisce Int32.MaxValue, ovvero System.Int32.MinValue - 1
.
Questo metodo è un'operazione O(n
), dove n
è count
.
In .NET Framework 2.0 e versioni successive questo metodo usa i Equals metodi e CompareTo di Array per determinare se l'oggetto Objectvalue
specificato dal parametro esiste. Nelle versioni precedenti di .NET Framework, questa determinazione è stata effettuata usando i Equals metodi e CompareTo di value
Object se stesso.
Vedi anche
- IndexOf
- Esecuzione di operazioni sulle stringhe indipendenti dalle impostazioni cultura nelle matrici
Si applica a
LastIndexOf<T>(T[], T)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intero oggetto 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
Parametri di tipo
- T
Tipo degli elementi della matrice.
Parametri
- array
- T[]
Oggetto Array unidimensionale in base zero in cui eseguire la ricerca.
- value
- T
Oggetto da trovare in array
.
Restituisce
Indice in base zero dell'ultima occorrenza dell'oggetto value
all'interno dell'intero oggetto array
, se trovato. In caso contrario, -1.
Eccezioni
array
è null
.
Esempio
Nell'esempio di codice seguente vengono illustrati tutti e tre gli overload generici del LastIndexOf metodo . Viene creata una matrice di stringhe, con una voce che viene visualizzata due volte, nella posizione di indice 0 e nella posizione di indice 5. L'overload del LastIndexOf<T>(T[], T) metodo cerca l'intera matrice dalla fine e trova la seconda occorrenza della stringa. L'overload del LastIndexOf<T>(T[], T, Int32) metodo viene usato per cercare la matrice all'indietro a partire dalla posizione di indice 3 e continuare fino all'inizio della matrice e trovare la prima occorrenza della stringa. Infine, l'overload del LastIndexOf<T>(T[], T, Int32, Int32) metodo viene usato per eseguire una ricerca in un intervallo di quattro voci, a partire dalla posizione di indice 4 ed estendendo all'indietro (ovvero cerca gli elementi in posizioni 4, 3, 2 e 1). Questa ricerca restituisce -1 perché non sono presenti istanze della stringa di ricerca in tale intervallo.
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
*/
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
Commenti
Viene Array eseguita la ricerca all'indietro a partire dall'ultimo elemento e termina con il primo elemento.
Gli elementi vengono confrontati con il valore specificato usando il Object.Equals metodo . Se il tipo di elemento è un tipo non intrinseco (definito dall'utente), viene usata l'implementazione Equals
di tale tipo.
Questo metodo è un'operazione O(n
), dove n
è l'oggetto Length di array
.
Vedi anche
- IndexOf
- Esecuzione di operazioni sulle stringhe indipendenti dalle impostazioni cultura nelle matrici
Si applica a
LastIndexOf<T>(T[], T, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi dell'oggetto Array compreso tra il primo elemento e l'indice specificato.
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
Parametri di tipo
- T
Tipo degli elementi della matrice.
Parametri
- array
- T[]
Oggetto Array unidimensionale in base zero in cui eseguire la ricerca.
- value
- T
Oggetto da trovare in array
.
- startIndex
- Int32
Indice iniziale in base zero della ricerca all'indietro.
Restituisce
Indice in base zero dell'ultima occorrenza di value
all'interno dell'intervallo di elementi di array
compreso tra il primo elemento e startIndex
, se trovato. In caso contrario, -1.
Eccezioni
array
è null
.
startIndex
non è compreso nell'intervallo di indici validi per array
.
Esempio
Nell'esempio di codice seguente vengono illustrati tutti e tre gli overload generici del LastIndexOf metodo . Viene creata una matrice di stringhe, con una voce che viene visualizzata due volte, nella posizione di indice 0 e nella posizione di indice 5. L'overload del LastIndexOf<T>(T[], T) metodo cerca l'intera matrice dalla fine e trova la seconda occorrenza della stringa. L'overload del LastIndexOf<T>(T[], T, Int32) metodo viene usato per cercare la matrice all'indietro a partire dalla posizione di indice 3 e continuare fino all'inizio della matrice e trovare la prima occorrenza della stringa. Infine, l'overload del LastIndexOf<T>(T[], T, Int32, Int32) metodo viene usato per eseguire una ricerca in un intervallo di quattro voci, a partire dalla posizione di indice 4 ed estendendo all'indietro (ovvero cerca gli elementi in posizioni 4, 3, 2 e 1). Questa ricerca restituisce -1 perché non sono presenti istanze della stringa di ricerca in tale intervallo.
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
*/
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
Commenti
L'oggetto Array viene cercato all'indietro a startIndex
partire da e terminando al primo elemento.
Gli elementi vengono confrontati con il valore specificato usando il Object.Equals metodo . Se il tipo di elemento è un tipo nontrinsico (definito dall'utente), viene usata l'implementazione Equals
di tale tipo.
Questo metodo è un'operazione O(n
), dove n
è il numero di elementi dall'inizio a array
startIndex
.
Vedi anche
- IndexOf
- Esecuzione di operazioni sulle stringhe indipendenti dalle impostazioni cultura nelle matrici
Si applica a
LastIndexOf<T>(T[], T, Int32, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice dell'ultima occorrenza all'interno dell'intervallo di elementi dell'oggetto Array che contiene il numero specificato di elementi e termina in corrispondenza dell'indice specificato.
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
Parametri di tipo
- T
Tipo degli elementi della matrice.
Parametri
- array
- T[]
Oggetto Array unidimensionale in base zero in cui eseguire la ricerca.
- value
- T
Oggetto da trovare in array
.
- startIndex
- Int32
Indice iniziale in base zero della ricerca all'indietro.
- count
- Int32
Numero di elementi nella sezione in cui eseguire la ricerca.
Restituisce
Indice in base zero dell'ultima occorrenza dell'oggetto value
all'interno dell'intervallo di elementi di array
che contiene il numero di elementi specificato in count
e termina in corrispondenza di startIndex
, se trovato. In caso contrario, -1.
Eccezioni
array
è null
.
startIndex
non è compreso nell'intervallo di indici validi per array
.
-oppure-
count
è minore di zero.
-oppure-
startIndex
e count
non specificano una sezione valida in array
.
Esempio
Nell'esempio di codice seguente vengono illustrati tutti e tre gli overload generici del LastIndexOf metodo. Viene creata una matrice di stringhe, con una voce visualizzata due volte, in posizione di indice 0 e posizione di indice 5. L'overload LastIndexOf<T>(T[], T) del metodo esegue la ricerca dell'intera matrice dalla fine e trova la seconda occorrenza della stringa. L'overload LastIndexOf<T>(T[], T, Int32) del metodo viene usato per cercare la matrice all'indietro a partire dalla posizione dell'indice 3 e continuare all'inizio della matrice e trova la prima occorrenza della stringa. Infine, l'overload del LastIndexOf<T>(T[], T, Int32, Int32) metodo viene usato per cercare un intervallo di quattro voci, a partire dalla posizione di indice 4 ed estendendo all'indietro (ovvero, cerca gli elementi in posizioni 4, 3, 2 e 1); questa ricerca restituisce -1 perché non sono presenti istanze della stringa di ricerca in tale intervallo.
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
*/
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
Commenti
L'oggetto viene cercato all'indietro Array a partire da e terminando a startIndex
startIndex
meno count
più 1, se count
è maggiore di 0.
Gli elementi vengono confrontati con il valore specificato usando il Object.Equals metodo . Se il tipo di elemento è un tipo nontrinsico (definito dall'utente), viene usata l'implementazione Equals
di tale tipo.
Questo metodo è un'operazione O(n
), dove n
è count
.
Vedi anche
- IndexOf
- Esecuzione di operazioni sulle stringhe indipendenti dalle impostazioni cultura nelle matrici
Si applica a
Commenti e suggerimenti
https://aka.ms/ContentUserFeedback.
Presto disponibile: Nel corso del 2024 verranno gradualmente disattivati i problemi di GitHub come meccanismo di feedback per il contenuto e ciò verrà sostituito con un nuovo sistema di feedback. Per altre informazioni, vedereInvia e visualizza il feedback per