Auf Englisch lesen

Freigeben über


ArrayList.IndexOf Methode

Definition

Gibt den nullbasierten Index des ersten Vorkommens eines Werts in der ArrayList bzw. in einem Abschnitt davon zurück.

Überlädt

IndexOf(Object)

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des ersten Vorkommens innerhalb der gesamten ArrayList zurück.

IndexOf(Object, Int32)

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des ersten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der sich vom angegebenen Index bis zum letzten Element erstreckt.

IndexOf(Object, Int32, Int32)

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des ersten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der am angegebenen Index beginnt und die angegebene Anzahl von Elementen enthält.

IndexOf(Object)

Quelle:
ArrayList.cs
Quelle:
ArrayList.cs
Quelle:
ArrayList.cs

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des ersten Vorkommens innerhalb der gesamten ArrayList zurück.

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

Parameter

value
Object

Das Object, das in der ArrayList gesucht werden soll. Der Wert kann null sein.

Gibt zurück

Der nullbasierte Index des ggf. ersten Vorkommens von value in der gesamten ArrayList, andernfalls -1.

Implementiert

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie der Index des ersten Vorkommens eines angegebenen Elements bestimmt wird.

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.
*/

Hinweise

Die ArrayList wird nach vorne gesucht, beginnend beim ersten Element und endend beim letzten Element.

Diese Methode führt eine lineare Suche aus. Daher ist diese Methode ein O(n) Vorgang, wobei n ist Count.

Diese Methode bestimmt die Gleichheit, indem aufgerufen Object.Equalswird.

Ab dem .NET Framework 2.0 verwendet diese Methode die -Objekte Equals und CompareTo -Methoden item der Auflistung, um zu bestimmen, ob ein Element vorhanden ist. In den früheren Versionen des .NET Framework wurde diese Bestimmung mithilfe der Equals Methoden und CompareTo des item Parameters für die Objekte in der Auflistung vorgenommen.

Weitere Informationen

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.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)

Quelle:
ArrayList.cs
Quelle:
ArrayList.cs
Quelle:
ArrayList.cs

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des ersten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der sich vom angegebenen Index bis zum letzten Element erstreckt.

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

Parameter

value
Object

Das Object, das in der ArrayList gesucht werden soll. Der Wert kann null sein.

startIndex
Int32

Der nullbasierte Startindex für die Suche. 0 (null) ist in einer leeren Liste gültig.

Gibt zurück

Der nullbasierte Index des ersten Vorkommens von value innerhalb des Bereichs von Elementen in ArrayList, der sich von startIndex bis zum letzten Element erstreckt, sofern gefunden; andernfalls –1.

Ausnahmen

startIndex liegt außerhalb des Bereichs der gültigen Indizes für das ArrayList.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie der Index des ersten Vorkommens eines angegebenen Elements bestimmt wird.

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.
*/

Hinweise

Die ArrayList wird nach vorne gesucht, beginnend am startIndex letzten Element und endend.

Diese Methode führt eine lineare Suche aus. Daher ist diese Methode ein O(n) Vorgang, wobei n die Anzahl der Elemente von startIndex bis zum Ende des ArrayListist.

Diese Methode bestimmt die Gleichheit, indem aufgerufen Object.Equalswird.

Ab dem .NET Framework 2.0 verwendet diese Methode die -Objekte Equals und CompareTo -Methoden item der Auflistung, um zu bestimmen, ob ein Element vorhanden ist. In den früheren Versionen des .NET Framework wurde diese Bestimmung mithilfe der Equals Methoden und CompareTo des item Parameters für die Objekte in der Auflistung vorgenommen.

Weitere Informationen

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.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)

Quelle:
ArrayList.cs
Quelle:
ArrayList.cs
Quelle:
ArrayList.cs

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des ersten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der am angegebenen Index beginnt und die angegebene Anzahl von Elementen enthält.

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

Parameter

value
Object

Das Object, das in der ArrayList gesucht werden soll. Der Wert kann null sein.

startIndex
Int32

Der nullbasierte Startindex für die Suche. 0 (null) ist in einer leeren Liste gültig.

count
Int32

Die Anzahl der Elemente im zu durchsuchenden Abschnitt.

Gibt zurück

Der nullbasierte Index des ersten Vorkommens von value innerhalb des Bereichs von Elementen in ArrayList, der am startIndex beginnt und eine Anzahl von Elementen count enthält, sofern gefunden; andernfalls –1.

Ausnahmen

startIndex liegt außerhalb des Bereichs der gültigen Indizes für das ArrayList.

- oder -

count ist kleiner als Null.

- oder -

startIndex und count geben keinen gültigen Abschnitt in der ArrayList an.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie der Index des ersten Vorkommens eines angegebenen Elements bestimmt wird.

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.
*/

Hinweise

Die ArrayList wird vorwärts gesucht, beginnend bei startIndex und endet bei startIndex plus count minus 1, wenn count größer als 0 ist.

Diese Methode führt eine lineare Suche aus. Daher ist diese Methode ein O(n) Vorgang, wobei n ist count.

Diese Methode bestimmt die Gleichheit, indem aufgerufen Object.Equalswird.

Ab dem .NET Framework 2.0 verwendet diese Methode die -Objekte Equals und CompareTo -Methoden item der Auflistung, um zu bestimmen, ob ein Element vorhanden ist. In den früheren Versionen des .NET Framework wurde diese Bestimmung mithilfe der Equals Methoden und CompareTo des item Parameters für die Objekte in der Auflistung vorgenommen.

Weitere Informationen

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.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