Collection<T>.IndexOf(T) 方法

定义

搜索指定的对象,并返回整个 Collection<T> 中第一个匹配项的从零开始的索引。

public:
 virtual int IndexOf(T item);
public int IndexOf (T item);
abstract member IndexOf : 'T -> int
override this.IndexOf : 'T -> int
Public Function IndexOf (item As T) As Integer

参数

item
T

要在 Collection<T> 中定位的对象。 对于引用类型,该值可以为 null

返回

如果找到,则为整个 itemCollection<T> 第一个匹配项的从零开始的索引;否则为 -1。

实现

示例

下面的代码示例演示 的许多属性和方法 Collection<T>。 代码示例创建字符串集合,使用 Add 方法添加多个字符串,显示 Count,并列出字符串。 该示例使用 IndexOf 方法查找字符串的索引,并使用 Contains 方法来确定字符串是否在集合中。 该示例使用 Insert 方法插入字符串,并使用 C#) 索引器 (默认 Item[] 属性检索和设置字符串。 该示例使用 方法按字符串标识删除字符串, Remove 使用 方法按索引删除字符串 RemoveAt 。 最后, Clear 方法用于清除集合中的所有字符串。

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;

public ref class Demo
{
public:
    static void Main()
    {
        Collection<String^>^ dinosaurs = gcnew Collection<String^>();

        dinosaurs->Add("Psitticosaurus");
        dinosaurs->Add("Caudipteryx");
        dinosaurs->Add("Compsognathus");
        dinosaurs->Add("Muttaburrasaurus");

        Console::WriteLine("{0} dinosaurs:", dinosaurs->Count);
        Display(dinosaurs);

        Console::WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
            dinosaurs->IndexOf("Muttaburrasaurus"));

        Console::WriteLine("\nContains(\"Caudipteryx\"): {0}",
            dinosaurs->Contains("Caudipteryx"));

        Console::WriteLine("\nInsert(2, \"Nanotyrannus\")");
        dinosaurs->Insert(2, "Nanotyrannus");
        Display(dinosaurs);

        Console::WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);

        Console::WriteLine("\ndinosaurs[2] = \"Microraptor\"");
        dinosaurs[2] = "Microraptor";
        Display(dinosaurs);

        Console::WriteLine("\nRemove(\"Microraptor\")");
        dinosaurs->Remove("Microraptor");
        Display(dinosaurs);

        Console::WriteLine("\nRemoveAt(0)");
        dinosaurs->RemoveAt(0);
        Display(dinosaurs);

        Console::WriteLine("\ndinosaurs.Clear()");
        dinosaurs->Clear();
        Console::WriteLine("Count: {0}", dinosaurs->Count);
    }

private:
    static void Display(Collection<String^>^ cs)
    {
        Console::WriteLine();
        for each( String^ item in cs )
        {
            Console::WriteLine(item);
        }
    }
};

int main()
{
    Demo::Main();
}

/* This code example produces the following output:

4 dinosaurs:

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")

Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"

Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus

Remove("Microraptor")

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

RemoveAt(0)

Caudipteryx
Compsognathus
Muttaburrasaurus

dinosaurs.Clear()
Count: 0
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<string> dinosaurs = new Collection<string>();

        dinosaurs.Add("Psitticosaurus");
        dinosaurs.Add("Caudipteryx");
        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Muttaburrasaurus");

        Console.WriteLine("{0} dinosaurs:", dinosaurs.Count);
        Display(dinosaurs);

        Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
            dinosaurs.IndexOf("Muttaburrasaurus"));

        Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
            dinosaurs.Contains("Caudipteryx"));

        Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
        dinosaurs.Insert(2, "Nanotyrannus");
        Display(dinosaurs);

        Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);

        Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
        dinosaurs[2] = "Microraptor";
        Display(dinosaurs);

        Console.WriteLine("\nRemove(\"Microraptor\")");
        dinosaurs.Remove("Microraptor");
        Display(dinosaurs);

        Console.WriteLine("\nRemoveAt(0)");
        dinosaurs.RemoveAt(0);
        Display(dinosaurs);

        Console.WriteLine("\ndinosaurs.Clear()");
        dinosaurs.Clear();
        Console.WriteLine("Count: {0}", dinosaurs.Count);
    }

    private static void Display(Collection<string> cs)
    {
        Console.WriteLine();
        foreach( string item in cs )
        {
            Console.WriteLine(item);
        }
    }
}

/* This code example produces the following output:

4 dinosaurs:

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")

Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"

Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus

Remove("Microraptor")

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

RemoveAt(0)

Caudipteryx
Compsognathus
Muttaburrasaurus

dinosaurs.Clear()
Count: 0
 */
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class Demo
    
    Public Shared Sub Main() 

        Dim dinosaurs As New Collection(Of String)

        dinosaurs.Add("Psitticosaurus")
        dinosaurs.Add("Caudipteryx")
        dinosaurs.Add("Compsognathus")
        dinosaurs.Add("Muttaburrasaurus")

        Console.WriteLine("{0} dinosaurs:", dinosaurs.Count)
        Display(dinosaurs)
    
        Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _
            dinosaurs.IndexOf("Muttaburrasaurus"))

        Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _
            dinosaurs.Contains("Caudipteryx"))

        Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
        dinosaurs.Insert(2, "Nanotyrannus")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))

        Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
        dinosaurs(2) = "Microraptor"
        Display(dinosaurs)

        Console.WriteLine(vbLf & "Remove(""Microraptor"")")
        dinosaurs.Remove("Microraptor")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "RemoveAt(0)")
        dinosaurs.RemoveAt(0)
        Display(dinosaurs)
 
        Console.WriteLine(vbLf & "dinosaurs.Clear()")
        dinosaurs.Clear()
        Console.WriteLine("Count: {0}", dinosaurs.Count)

    End Sub
    
    Private Shared Sub Display(ByVal cs As Collection(Of String)) 
        Console.WriteLine()
        For Each item As String In cs
            Console.WriteLine(item)
        Next item
    End Sub
End Class

' This code example produces the following output:
'
'4 dinosaurs:
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'IndexOf("Muttaburrasaurus"): 3
'
'Contains("Caudipteryx"): True
'
'Insert(2, "Nanotyrannus")
'
'Psitticosaurus
'Caudipteryx
'Nanotyrannus
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs(2): Nanotyrannus
'
'dinosaurs(2) = "Microraptor"
'
'Psitticosaurus
'Caudipteryx
'Microraptor
'Compsognathus
'Muttaburrasaurus
'
'Remove("Microraptor")
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'RemoveAt(0)
'
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs.Clear()
'Count: 0

注解

Collection<T> 第一个元素开始,在最后一个元素处结束,向前搜索 。

此方法使用 的默认相等比较器EqualityComparer<T>.DefaultT确定相等性,即列表中的值类型。

此方法执行线性搜索;因此,平均执行时间与 Count成正比。 也就是说,此方法是 O (n) 操作,其中 nCount

适用于

另请参阅