ArrayList.IndexOf Metodo

Definizione

Restituisce l'indice in base zero della prima occorrenza di un valore nel ArrayList o in una parte di esso.

Overload

IndexOf(Object)

Cerca il Object specificato e restituisce l'indice in base zero della prima occorrenza all'interno dell'intero ArrayList.

IndexOf(Object, Int32)

Cerca il Object specificato e restituisce l'indice in base zero della prima occorrenza all'interno dell'intervallo di elementi nella ArrayList che si estende dall'indice specificato all'ultimo elemento.

IndexOf(Object, Int32, Int32)

Cerca il Object specificato e restituisce l'indice in base zero della prima occorrenza all'interno dell'intervallo di elementi nella ArrayList che inizia in corrispondenza dell'indice specificato e contiene il numero specificato di elementi.

IndexOf(Object)

Origine:
ArrayList.cs
Origine:
ArrayList.cs
Origine:
ArrayList.cs

Cerca il Object specificato e restituisce l'indice in base zero della prima occorrenza all'interno dell'intero ArrayList.

C#
public virtual int IndexOf (object value);
C#
public virtual int IndexOf (object? value);

Parametri

value
Object

Object da individuare nel ArrayList. Il valore può essere null.

Restituisce

Indice in base zero della prima occorrenza di value all'interno dell'intero ArrayList, se trovato; in caso contrario, -1.

Implementazioni

Esempio

Nell'esempio di codice seguente viene illustrato come determinare l'indice della prima occorrenza di un elemento specificato.

C#
using System;
using System.Collections;
public class SamplesArrayList
{

    public static void Main()
    {

        // Creates and initializes a new ArrayList with three elements of the same value.
        ArrayList myAL = new ArrayList();
        myAL.Add( "the" );
        myAL.Add( "quick" );
        myAL.Add( "brown" );
        myAL.Add( "fox" );
        myAL.Add( "jumps" );
        myAL.Add( "over" );
        myAL.Add( "the" );
        myAL.Add( "lazy" );
        myAL.Add( "dog" );
        myAL.Add( "in" );
        myAL.Add( "the" );
        myAL.Add( "barn" );

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

        // Search for the first occurrence of the duplicated value.
        string myString = "the";
        int myIndex = myAL.IndexOf( myString );
        Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in the last section of the ArrayList.
        myIndex = myAL.IndexOf( myString, 4 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in a section of the ArrayList.
        myIndex = myAL.IndexOf( myString, 6, 6 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList.
        myIndex = myAL.IndexOf( myString, 11 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 11 and the end is at index {1}.", myString, myIndex );
    }

    public static void PrintIndexAndValues(IEnumerable myList)
    {
        int i = 0;
        foreach (Object obj in myList)
            Console.WriteLine("   [{0}]:    {1}", i++, obj);
        Console.WriteLine();
    }
}
/*
This code produces output similar to the following:

The ArrayList 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 6 and index 11 is at index 6.
The first occurrence of "the" between index 11 and the end is at index -1.
*/

Commenti

Il ArrayList viene cercato in avanti a partire dal primo elemento e termina con l'ultimo elemento.

Questo metodo esegue una ricerca lineare; pertanto, questo metodo è un'operazione di O(n), in cui n è Count.

Questo metodo determina l'uguaglianza chiamando Object.Equals.

A partire da .NET Framework 2.0, questo metodo usa i metodi Equals e CompareTo degli oggetti della raccolta in item per determinare se l'elemento esiste. Nelle versioni precedenti di .NET Framework questa determinazione è stata effettuata usando i metodi Equals e CompareTo del parametro item sugli oggetti dell'insieme.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

IndexOf(Object, Int32)

Origine:
ArrayList.cs
Origine:
ArrayList.cs
Origine:
ArrayList.cs

Cerca il Object specificato e restituisce l'indice in base zero della prima occorrenza all'interno dell'intervallo di elementi nella ArrayList che si estende dall'indice specificato all'ultimo elemento.

C#
public virtual int IndexOf (object value, int startIndex);
C#
public virtual int IndexOf (object? value, int startIndex);

Parametri

value
Object

Object da individuare nel ArrayList. Il valore può essere null.

startIndex
Int32

Indice iniziale in base zero della ricerca. 0 (zero) è valido in un elenco vuoto.

Restituisce

Indice in base zero della prima occorrenza di value all'interno dell'intervallo di elementi nel ArrayList che si estende da startIndex all'ultimo elemento, se trovato; in caso contrario, -1.

Eccezioni

startIndex non è compreso nell'intervallo di indici validi per l'ArrayList.

Esempio

Nell'esempio di codice seguente viene illustrato come determinare l'indice della prima occorrenza di un elemento specificato.

C#
using System;
using System.Collections;
public class SamplesArrayList
{

    public static void Main()
    {

        // Creates and initializes a new ArrayList with three elements of the same value.
        ArrayList myAL = new ArrayList();
        myAL.Add( "the" );
        myAL.Add( "quick" );
        myAL.Add( "brown" );
        myAL.Add( "fox" );
        myAL.Add( "jumps" );
        myAL.Add( "over" );
        myAL.Add( "the" );
        myAL.Add( "lazy" );
        myAL.Add( "dog" );
        myAL.Add( "in" );
        myAL.Add( "the" );
        myAL.Add( "barn" );

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

        // Search for the first occurrence of the duplicated value.
        string myString = "the";
        int myIndex = myAL.IndexOf( myString );
        Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in the last section of the ArrayList.
        myIndex = myAL.IndexOf( myString, 4 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in a section of the ArrayList.
        myIndex = myAL.IndexOf( myString, 6, 6 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList.
        myIndex = myAL.IndexOf( myString, 11 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 11 and the end is at index {1}.", myString, myIndex );
    }

    public static void PrintIndexAndValues(IEnumerable myList)
    {
        int i = 0;
        foreach (Object obj in myList)
            Console.WriteLine("   [{0}]:    {1}", i++, obj);
        Console.WriteLine();
    }
}
/*
This code produces output similar to the following:

The ArrayList 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 6 and index 11 is at index 6.
The first occurrence of "the" between index 11 and the end is at index -1.
*/

Commenti

Il ArrayList viene cercato in avanti a partire da startIndex e termina con l'ultimo elemento.

Questo metodo esegue una ricerca lineare; pertanto, questo metodo è un'operazione di O(n), dove n è il numero di elementi da startIndex alla fine del ArrayList.

Questo metodo determina l'uguaglianza chiamando Object.Equals.

A partire da .NET Framework 2.0, questo metodo usa i metodi Equals e CompareTo degli oggetti della raccolta in item per determinare se l'elemento esiste. Nelle versioni precedenti di .NET Framework questa determinazione è stata effettuata usando i metodi Equals e CompareTo del parametro item sugli oggetti dell'insieme.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

IndexOf(Object, Int32, Int32)

Origine:
ArrayList.cs
Origine:
ArrayList.cs
Origine:
ArrayList.cs

Cerca il Object specificato e restituisce l'indice in base zero della prima occorrenza all'interno dell'intervallo di elementi nella ArrayList che inizia in corrispondenza dell'indice specificato e contiene il numero specificato di elementi.

C#
public virtual int IndexOf (object value, int startIndex, int count);
C#
public virtual int IndexOf (object? value, int startIndex, int count);

Parametri

value
Object

Object da individuare nel ArrayList. Il valore può essere null.

startIndex
Int32

Indice iniziale in base zero della ricerca. 0 (zero) è valido in un elenco vuoto.

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 nel ArrayList che inizia a startIndex e contiene count numero di elementi, se trovato; in caso contrario, -1.

Eccezioni

startIndex non è compreso nell'intervallo di indici validi per l'ArrayList.

-o-

count è minore di zero.

-o-

startIndex e count non specificano una sezione valida nel ArrayList.

Esempio

Nell'esempio di codice seguente viene illustrato come determinare l'indice della prima occorrenza di un elemento specificato.

C#
using System;
using System.Collections;
public class SamplesArrayList
{

    public static void Main()
    {

        // Creates and initializes a new ArrayList with three elements of the same value.
        ArrayList myAL = new ArrayList();
        myAL.Add( "the" );
        myAL.Add( "quick" );
        myAL.Add( "brown" );
        myAL.Add( "fox" );
        myAL.Add( "jumps" );
        myAL.Add( "over" );
        myAL.Add( "the" );
        myAL.Add( "lazy" );
        myAL.Add( "dog" );
        myAL.Add( "in" );
        myAL.Add( "the" );
        myAL.Add( "barn" );

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

        // Search for the first occurrence of the duplicated value.
        string myString = "the";
        int myIndex = myAL.IndexOf( myString );
        Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in the last section of the ArrayList.
        myIndex = myAL.IndexOf( myString, 4 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in a section of the ArrayList.
        myIndex = myAL.IndexOf( myString, 6, 6 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );

        // Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList.
        myIndex = myAL.IndexOf( myString, 11 );
        Console.WriteLine( "The first occurrence of \"{0}\" between index 11 and the end is at index {1}.", myString, myIndex );
    }

    public static void PrintIndexAndValues(IEnumerable myList)
    {
        int i = 0;
        foreach (Object obj in myList)
            Console.WriteLine("   [{0}]:    {1}", i++, obj);
        Console.WriteLine();
    }
}
/*
This code produces output similar to the following:

The ArrayList 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 6 and index 11 is at index 6.
The first occurrence of "the" between index 11 and the end is at index -1.
*/

Commenti

Il ArrayList viene cercato in avanti a partire da startIndex e termina a startIndex più count meno 1, se count è maggiore di 0.

Questo metodo esegue una ricerca lineare; pertanto, questo metodo è un'operazione di O(n), in cui n è count.

Questo metodo determina l'uguaglianza chiamando Object.Equals.

A partire da .NET Framework 2.0, questo metodo usa i metodi Equals e CompareTo degli oggetti della raccolta in item per determinare se l'elemento esiste. Nelle versioni precedenti di .NET Framework questa determinazione è stata effettuata usando i metodi Equals e CompareTo del parametro item sugli oggetti dell'insieme.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0