ArrayList.IndexOf 方法

定义

返回 ArrayList 或部分值第一个匹配项的从零开始的索引。

重载

IndexOf(Object)

搜索指定的 Object,并返回整个 ArrayList中第一个匹配项的从零开始的索引。

IndexOf(Object, Int32)

搜索指定的 Object,并返回从指定索引扩展到最后一个元素的 ArrayList 中第一个匹配项的从零开始的索引。

IndexOf(Object, Int32, Int32)

搜索指定的 Object,并返回从指定索引开始且包含指定数量的元素的 ArrayList 中第一个匹配项的从零开始的索引。

IndexOf(Object)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

搜索指定的 Object,并返回整个 ArrayList中第一个匹配项的从零开始的索引。

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

参数

value
Object

ArrayList中找到的 Object。 该值可以 null

返回

如果找到,则为整个 ArrayList中第一个匹配项 value 的从零开始的索引;否则为 -1。

实现

示例

下面的代码示例演示如何确定指定元素的第一个匹配项的索引。

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

注解

从第一个元素开始搜索 ArrayList,最后一个元素结束。

此方法执行线性搜索;因此,此方法是 O(n) 操作,其中 nCount

此方法通过调用 Object.Equals来确定相等性。

从 .NET Framework 2.0 开始,此方法使用集合对象的 EqualsCompareTo 方法 item 来确定项是否存在。 在 .NET Framework 的早期版本中,使用集合中对象的 EqualsCompareTo 方法 item 参数的 CompareTo 方法做出了此决定。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

搜索指定的 Object,并返回从指定索引扩展到最后一个元素的 ArrayList 中第一个匹配项的从零开始的索引。

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

参数

value
Object

ArrayList中找到的 Object。 该值可以 null

startIndex
Int32

从零开始的搜索索引。 0 (零)在空列表中有效。

返回

在从 startIndex 扩展到最后一个元素的 ArrayList 元素范围内 value 的第一个匹配项的从零开始的索引(如果找到);否则为 -1。

例外

startIndex 超出了 ArrayList的有效索引范围。

示例

下面的代码示例演示如何确定指定元素的第一个匹配项的索引。

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

注解

startIndex 开始搜索 ArrayList,最后一个元素结束。

此方法执行线性搜索;因此,此方法是一个 O(n) 操作,其中 nstartIndexArrayList末尾的元素数。

此方法通过调用 Object.Equals来确定相等性。

从 .NET Framework 2.0 开始,此方法使用集合对象的 EqualsCompareTo 方法 item 来确定项是否存在。 在 .NET Framework 的早期版本中,使用集合中对象的 EqualsCompareTo 方法 item 参数的 CompareTo 方法做出了此决定。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

搜索指定的 Object,并返回从指定索引开始且包含指定数量的元素的 ArrayList 中第一个匹配项的从零开始的索引。

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

参数

value
Object

ArrayList中找到的 Object。 该值可以 null

startIndex
Int32

从零开始的搜索索引。 0 (零)在空列表中有效。

count
Int32

要搜索的节中的元素数。

返回

ArrayList 中从 startIndex 开始且包含 count 个元素数(如果找到)中 value 的第一个匹配项的从零开始的索引;否则为 -1。

例外

startIndex 超出了 ArrayList的有效索引范围。

-或-

count 小于零。

-或-

startIndexcount 未在 ArrayList中指定有效的节。

示例

下面的代码示例演示如何确定指定元素的第一个匹配项的索引。

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

注解

如果 count 大于 0,则从 startIndex 开始搜索 ArrayList,以 startIndexcount 减 1 结束。

此方法执行线性搜索;因此,此方法是 O(n) 操作,其中 ncount

此方法通过调用 Object.Equals来确定相等性。

从 .NET Framework 2.0 开始,此方法使用集合对象的 EqualsCompareTo 方法 item 来确定项是否存在。 在 .NET Framework 的早期版本中,使用集合中对象的 EqualsCompareTo 方法 item 参数的 CompareTo 方法做出了此决定。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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