Array.IndexOf 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.
Cerca l'oggetto specificato e restituisce l'indice della prima occorrenza in una matrice unidimensionale o in un intervallo di elementi nella matrice.
Overload
| Nome | Descrizione |
|---|---|
| IndexOf(Array, Object) |
Cerca l'oggetto specificato e restituisce l'indice della prima occorrenza in una matrice unidimensionale. |
| IndexOf(Array, Object, Int32) |
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice della prima occorrenza. L'intervallo si estende da un indice specificato alla fine della matrice. |
| IndexOf(Array, Object, Int32, Int32) |
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice di ifs prima occorrenza. L'intervallo si estende da un indice specificato per un numero specificato di elementi. |
| IndexOf<T>(T[], T, Int32) |
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice della prima occorrenza. L'intervallo si estende da un indice specificato alla fine della matrice. |
| IndexOf<T>(T[], T, Int32, Int32) |
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice della prima occorrenza. L'intervallo si estende da un indice specificato per un numero specificato di elementi. |
| IndexOf<T>(T[], T) |
Cerca l'oggetto specificato e restituisce l'indice della prima occorrenza in una matrice unidimensionale. |
IndexOf(Array, Object)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice della prima occorrenza in una matrice unidimensionale.
public:
static int IndexOf(Array ^ array, System::Object ^ value);
public static int IndexOf(Array array, object value);
public static int IndexOf(Array array, object? value);
static member IndexOf : Array * obj -> int
Public Shared Function IndexOf (array As Array, value As Object) As Integer
Parametri
- array
- Array
Matrice unidimensionale da cercare.
- value
- Object
Oggetto da individuare in array.
Restituisce
Indice della prima occorrenza di value in array, se trovato; in caso contrario, limite inferiore della matrice meno 1.
Eccezioni
array è null.
array è multidimensionale.
Esempio
Nell'esempio vengono chiamati i tre overload seguenti del IndexOf metodo per trovare l'indice di una stringa in una matrice di stringhe:
IndexOf(Array, Object)per determinare la prima occorrenza della stringa "the" in una matrice di stringhe.
IndexOf(Array, Object, Int32), per determinare la prima occorrenza della stringa "the" nel quarto all'ultimo elemento di una matrice di stringhe.
IndexOf(Array, Object, Int32, Int32), per determinare la prima occorrenza della stringa "the" in una matrice di stringhe dall'elemento che segue l'ultima corrispondenza riuscita alla fine della matrice.
// Create a string array with 3 elements having the same value.
let strings =
[| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
"the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
printfn $" [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays 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 first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index);
// The example displays 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 first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = Array.IndexOf(strings, searchString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in a section of the array.
Dim position As Integer = index + 1
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index)
End Sub
End Module
' The example displays 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 first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 7 and index 11 is at index 10.
Commenti
Questo metodo cerca tutti gli elementi di una matrice unidimensionale per value. Per determinare se value esiste in array, il metodo esegue un confronto di uguaglianza usando l'operatore di confronto EqualityComparer<T>.Defaultdi uguaglianza predefinito .
Poiché la maggior parte delle matrici ha un limite inferiore pari a zero, questo metodo restituisce in genere -1 sevalue non viene trovato. Nel raro caso in cui il limite inferiore della matrice sia uguale a Int32.MinValue(0x80000000) e value non venga trovato, questo metodo restituisce Int32.MaxValue (0x7FFFFFFF).
Questo metodo è un'operazione O(n), dove n è l'oggetto Length di array.
Vedi anche
Si applica a
IndexOf(Array, Object, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice della prima occorrenza. L'intervallo si estende da un indice specificato alla fine della matrice.
public:
static int IndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int IndexOf(Array array, object value, int startIndex);
public static int IndexOf(Array array, object? value, int startIndex);
static member IndexOf : Array * obj * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer) As Integer
Parametri
- array
- Array
Matrice unidimensionale da cercare.
- value
- Object
Oggetto da individuare in array.
- startIndex
- Int32
Indice iniziale della ricerca. 0 (zero) è valido in una matrice vuota.
Restituisce
Indice della prima occorrenza di value, se trovato, all'interno dell'intervallo di elementi in array che si estende dall'ultimo startIndex elemento; in caso contrario, 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 vengono chiamati i tre overload seguenti del IndexOf metodo per trovare l'indice di una stringa in una matrice di stringhe:
IndexOf(Array, Object)per determinare la prima occorrenza della stringa "the" in una matrice di stringhe.
IndexOf(Array, Object, Int32), per determinare la prima occorrenza della stringa "the" nel quarto all'ultimo elemento di una matrice di stringhe.
IndexOf(Array, Object, Int32, Int32), per determinare la prima occorrenza della stringa "the" in una matrice di stringhe dall'elemento che segue l'ultima corrispondenza riuscita alla fine della matrice.
// Create a string array with 3 elements having the same value.
let strings =
[| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
"the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
printfn $" [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays 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 first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index);
// The example displays 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 first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = Array.IndexOf(strings, searchString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in a section of the array.
Dim position As Integer = index + 1
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index)
End Sub
End Module
' The example displays 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 first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 7 and index 11 is at index 10.
Commenti
Questo metodo cerca una matrice unidimensionale dall'elemento in corrispondenza dell'indice startIndex all'ultimo elemento. Per determinare se value esiste in array, il metodo esegue un confronto di uguaglianza usando l'operatore di confronto EqualityComparer<T>.Defaultdi uguaglianza predefinito .
Poiché la maggior parte delle matrici ha un limite inferiore pari a zero, questo metodo restituisce in genere -1 se value non viene trovato. Nel raro caso in cui il limite inferiore della matrice sia uguale a Int32.MinValue(0x80000000) e value non venga trovato, questo metodo restituisce Int32.MaxValue (0x7FFFFFFF).
Se startIndex è uguale a , il metodo restituisce Array.Length-1. Se startIndex è maggiore di Array.Length, il metodo genera un'eccezione ArgumentOutOfRangeException.
Questo metodo è un'operazione O(n), dove n è il numero di elementi da startIndex alla fine di array.
Vedi anche
Si applica a
IndexOf(Array, Object, Int32, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice di ifs prima occorrenza. L'intervallo si estende da un indice specificato per un numero specificato di elementi.
public:
static int IndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int IndexOf(Array array, object value, int startIndex, int count);
public static int IndexOf(Array array, object? value, int startIndex, int count);
static member IndexOf : Array * obj * int * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer
Parametri
- array
- Array
Matrice unidimensionale da cercare.
- value
- Object
Oggetto da individuare in array.
- startIndex
- Int32
Indice iniziale della ricerca. 0 (zero) è valido in una matrice vuota.
- count
- Int32
Numero di elementi da cercare.
Restituisce
Indice della prima occorrenza di value, se presente nell'oggetto dall'indice arraystartIndex a startIndex + count - 1; in caso contrario, 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 vengono chiamati i tre overload seguenti del IndexOf metodo per trovare l'indice di una stringa in una matrice di stringhe:
IndexOf(Array, Object)per determinare la prima occorrenza della stringa "the" in una matrice di stringhe.
IndexOf(Array, Object, Int32), per determinare la prima occorrenza della stringa "the" nel quarto all'ultimo elemento di una matrice di stringhe.
IndexOf(Array, Object, Int32, Int32), per determinare la prima occorrenza della stringa "the" in una matrice di stringhe dall'elemento che segue l'ultima corrispondenza riuscita alla fine della matrice. Per determinare il valore dell'argomento
count, sottrae il limite superiore della matrice dall'indice iniziale e ne aggiunge uno.
// Create a string array with 3 elements having the same value.
let strings =
[| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
"the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
printfn $" [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays 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 first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index);
// The example displays 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 first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = Array.IndexOf(strings, searchString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in a section of the array.
Dim position As Integer = index + 1
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index)
End Sub
End Module
' The example displays 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 first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 7 and index 11 is at index 10.
Commenti
Questo metodo cerca gli elementi di una matrice unidimensionale da startIndex a startIndex più count meno 1, se count è maggiore di 0. Per determinare se value esiste in array, il metodo esegue un confronto di uguaglianza usando l'operatore di confronto EqualityComparer<T>.Defaultdi uguaglianza predefinito .
Poiché la maggior parte delle matrici ha un limite inferiore pari a zero, questo metodo restituisce in genere -1 quando value non viene trovato. Nel raro caso in cui il limite inferiore della matrice sia uguale a Int32.MinValue (0x80000000) e value non venga trovato, questo metodo restituisce Int32.MaxValue (0x7FFFFFFF).
Se startindex è uguale Array.Lengtha , il metodo restituisce -1. Se startIndex è maggiore di Array.Length, il metodo genera un'eccezione ArgumentOutOfRangeException.
Questo metodo è un'operazione O(n), dove n è count.
Vedi anche
Si applica a
IndexOf<T>(T[], T, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice della prima occorrenza. L'intervallo si estende da un indice specificato alla fine della matrice.
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int IndexOf<T>(T[] array, T value, int startIndex);
static member IndexOf : 'T[] * 'T * int -> int
Public Shared Function IndexOf(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[]
Matrice unidimensionale in base zero da cercare.
- value
- T
Oggetto da individuare in array.
- startIndex
- Int32
Indice iniziale in base zero della ricerca. 0 (zero) è valido in una matrice vuota.
Restituisce
Indice in base zero della prima occorrenza di value all'interno dell'intervallo di elementi in array che si estende dall'ultimo startIndex elemento, se trovato; in caso contrario, -1.
Eccezioni
array è null.
startIndex non è compreso nell'intervallo di indici validi per array.
Esempio
Nell'esempio seguente vengono illustrati tutti e tre gli overload generici del IndexOf metodo . Viene creata una matrice di stringhe, con una voce che viene visualizzata due volte, in corrispondenza della posizione di indice 0 e della posizione di indice 5. L'overload del IndexOf<T>(T[], T) metodo cerca la matrice dall'inizio e trova la prima occorrenza della stringa. L'overload IndexOf<T>(T[], T, Int32) del metodo viene usato per cercare la matrice a partire dalla posizione di indice 3 e continuare fino alla fine della matrice e trova la seconda occorrenza della stringa. Infine, l'overload del IndexOf<T>(T[], T, Int32, Int32) metodo viene usato per eseguire ricerche in un intervallo di due voci, a partire dalla posizione dell'indice due. Restituisce -1 perché non sono presenti istanze della stringa di ricerca in tale intervallo.
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -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.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Commenti
Questo metodo cerca una matrice unidimensionale dall'elemento alla startIndex fine della matrice. Per determinare se value esiste in array, il metodo esegue un confronto di uguaglianza usando l'operatore di confronto EqualityComparer<T>.Defaultdi uguaglianza predefinito .
Se startIndex è uguale a , il metodo restituisce Length-1. Se startIndex è maggiore di Array.Length, il metodo genera un'eccezione ArgumentOutOfRangeException.
Questo metodo è un'operazione O(n), dove n è il numero di elementi da startIndex alla fine di array.
Vedi anche
Si applica a
IndexOf<T>(T[], T, Int32, Int32)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato in un intervallo di elementi di una matrice unidimensionale e restituisce l'indice della prima occorrenza. L'intervallo si estende da un indice specificato per un numero specificato di elementi.
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int IndexOf<T>(T[] array, T value, int startIndex, int count);
static member IndexOf : 'T[] * 'T * int * int -> int
Public Shared Function IndexOf(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[]
Matrice unidimensionale in base zero da cercare.
- value
- T
Oggetto da individuare in array.
- startIndex
- Int32
Indice iniziale in base zero della ricerca. 0 (zero) è valido in una matrice vuota.
- count
- Int32
Numero di elementi nella sezione da cercare.
Restituisce
Indice in base zero della prima occorrenza di value all'interno dell'intervallo di elementi in che array inizia da startIndex e contiene il numero di elementi specificati in count, 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 seguente vengono illustrati tutti e tre gli overload generici del IndexOf metodo . Viene creata una matrice di stringhe, con una voce che viene visualizzata due volte, in corrispondenza della posizione di indice 0 e della posizione di indice 5. L'overload del IndexOf<T>(T[], T) metodo cerca la matrice dall'inizio e trova la prima occorrenza della stringa. L'overload IndexOf<T>(T[], T, Int32) del metodo viene usato per cercare la matrice a partire dalla posizione di indice 3 e continuare fino alla fine della matrice e trova la seconda occorrenza della stringa. Infine, l'overload del IndexOf<T>(T[], T, Int32, Int32) metodo viene usato per eseguire ricerche in un intervallo di due voci, a partire dalla posizione dell'indice due. Restituisce -1 perché non sono presenti istanze della stringa di ricerca in tale intervallo.
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -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.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Commenti
Questo metodo cerca gli elementi di una matrice unidimensionale da startIndex a startIndex più count meno 1, se count è maggiore di 0. Per determinare se value esiste in array, il metodo esegue un confronto di uguaglianza usando l'operatore di confronto EqualityComparer<T>.Defaultdi uguaglianza predefinito .
Se startIndex è uguale Array.Lengtha , il metodo restituisce -1. Se startIndex è maggiore di Array.Length, il metodo genera un'eccezione ArgumentOutOfRangeException.
Questo metodo è un'operazione O(n), dove n è count.
Vedi anche
Si applica a
IndexOf<T>(T[], T)
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
- Origine:
- Array.cs
Cerca l'oggetto specificato e restituisce l'indice della prima occorrenza in una matrice unidimensionale.
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value);
public static int IndexOf<T>(T[] array, T value);
static member IndexOf : 'T[] * 'T -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T) As Integer
Parametri di tipo
- T
Tipo degli elementi della matrice.
Parametri
- array
- T[]
Matrice unidimensionale in base zero da cercare.
- value
- T
Oggetto da individuare in array.
Restituisce
Indice in base zero della prima occorrenza di value nell'intero arrayoggetto , se trovato; in caso contrario, -1.
Eccezioni
array è null.
Esempio
Nell'esempio seguente vengono illustrati tutti e tre gli overload generici del IndexOf metodo . Viene creata una matrice di stringhe, con una voce che viene visualizzata due volte, in corrispondenza della posizione di indice 0 e della posizione di indice 5. L'overload del IndexOf<T>(T[], T) metodo cerca la matrice dall'inizio e trova la prima occorrenza della stringa. L'overload IndexOf<T>(T[], T, Int32) del metodo viene usato per cercare la matrice a partire dalla posizione di indice 3 e continuare fino alla fine della matrice e trova la seconda occorrenza della stringa. Infine, l'overload del IndexOf<T>(T[], T, Int32, Int32) metodo viene usato per eseguire ricerche in un intervallo di due voci, a partire dalla posizione dell'indice due. Restituisce -1 perché non sono presenti istanze della stringa di ricerca in tale intervallo.
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -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.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Commenti
Questo metodo cerca tutti gli elementi di una matrice unidimensionale per value. Per determinare se value esiste in array, il metodo esegue un confronto di uguaglianza usando l'operatore di confronto EqualityComparer<T>.Defaultdi uguaglianza predefinito .
Questo metodo è un'operazione O(n), dove n è l'oggetto Length di array.