ArrayList.IndexOf 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回 ArrayList 或其中一部分中,第一次出現某值之以零為起始的索引。
多載
IndexOf(Object) | |
IndexOf(Object, Int32) |
在 ArrayList 中從指定的索引開始到最後一個項目這段範圍內,搜尋指定的 Object 第一次出現的位置,並傳回其索引值 (索引以零起始)。 |
IndexOf(Object, Int32, Int32) |
在 ArrayList 中從指定索引開始且包含指定項目個數的範圍內,搜尋指定的 Object 並傳回第一次出現的以零為起始的索引。 |
IndexOf(Object)
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
public:
virtual int IndexOf(System::Object ^ value);
public virtual int IndexOf (object value);
public virtual int IndexOf (object? value);
abstract member IndexOf : obj -> int
override this.IndexOf : obj -> int
Public Overridable Function IndexOf (value As Object) As Integer
參數
傳回
整個 value
中第一個出現 ArrayList 之以零為起始的索引 (如有找到),否則為 -1。
實作
範例
下列程式代碼範例示範如何判斷指定專案第一次出現的索引。
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList^ myAL = gcnew 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 );
}
void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( " [{0}]: {1}", i++, obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
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.
*/
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.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList with three elements of the same value.
Dim myAL As 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.
Dim myString As [String] = "the"
Dim myIndex As Integer = 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)
End Sub
Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer
Dim obj As [Object]
For Each obj In myList
Console.WriteLine(" [{0}]: {1}", i, obj)
i = i + 1
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' 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)
,其中 n
是 Count。
這個方法會呼叫 Object.Equals來判斷相等。
從 .NET Framework 2.0 開始,這個方法會使用 集合的物件 Equals 和 CompareTo 方法item
來判斷專案是否存在。 在舊版的 .NET Framework 中,已使用 Equals 集合中 物件的 和 CompareTo 方法item
,來判斷此判斷。
另請參閱
適用於
IndexOf(Object, Int32)
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
public:
virtual int IndexOf(System::Object ^ value, int startIndex);
public virtual int IndexOf (object value, int startIndex);
public virtual int IndexOf (object? value, int startIndex);
abstract member IndexOf : obj * int -> int
override this.IndexOf : obj * int -> int
Public Overridable Function IndexOf (value As Object, startIndex As Integer) As Integer
參數
- startIndex
- Int32
搜尋之以零為起始的起始索引。 0 (零) 在空白清單中有效。
傳回
在 ArrayList 中從 startIndex
開始到最後一個元素的範圍內,第一次出現 value
的位置之以零為起始的索引 (如有找到),如未找到則為 -1。
例外狀況
startIndex
超出 ArrayList 的有效索引範圍。
範例
下列程式代碼範例示範如何判斷指定專案第一次出現的索引。
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList^ myAL = gcnew 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 );
}
void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( " [{0}]: {1}", i++, obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
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.
*/
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.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList with three elements of the same value.
Dim myAL As 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.
Dim myString As [String] = "the"
Dim myIndex As Integer = 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)
End Sub
Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer
Dim obj As [Object]
For Each obj In myList
Console.WriteLine(" [{0}]: {1}", i, obj)
i = i + 1
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' 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 向前搜尋,從 startIndex
最後一個專案開始和結束。
這個方法會執行線性搜尋;因此,這個方法是作業 O(n)
,其中 n
是 從 startIndex
到 結尾的項目 ArrayList數目。
這個方法會呼叫 Object.Equals來判斷相等。
從 .NET Framework 2.0 開始,這個方法會使用 集合的物件 Equals 和 CompareTo 方法item
來判斷專案是否存在。 在舊版的 .NET Framework 中,已使用 Equals 集合中 物件的 和 CompareTo 方法item
,來判斷此判斷。
另請參閱
適用於
IndexOf(Object, Int32, Int32)
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
public:
virtual int IndexOf(System::Object ^ value, int startIndex, int count);
public virtual int IndexOf (object value, int startIndex, int count);
public virtual int IndexOf (object? value, int startIndex, int count);
abstract member IndexOf : obj * int * int -> int
override this.IndexOf : obj * int * int -> int
Public Overridable Function IndexOf (value As Object, startIndex As Integer, count As Integer) As Integer
參數
- startIndex
- Int32
搜尋之以零為起始的起始索引。 0 (零) 在空白清單中有效。
- count
- Int32
區段中要搜尋的項目數目。
傳回
在 ArrayList 中從 startIndex
開始且包含 count
個元素的範圍內,第一次出現 value
之以零為起始的索引,如未找到則為 -1。
例外狀況
範例
下列程式代碼範例示範如何判斷指定專案第一次出現的索引。
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList^ myAL = gcnew 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 );
}
void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( " [{0}]: {1}", i++, obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
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.
*/
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.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList with three elements of the same value.
Dim myAL As 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.
Dim myString As [String] = "the"
Dim myIndex As Integer = 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)
End Sub
Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer
Dim obj As [Object]
For Each obj In myList
Console.WriteLine(" [{0}]: {1}", i, obj)
i = i + 1
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' 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.
'
備註
如果 大於 0,則會 ArrayList 從 開始 startIndex
搜尋 ,結尾為 startIndex
加 count
減 count
1。
這個方法會執行線性搜尋;因此,這個方法是作業 O(n)
,其中 n
是 count
。
這個方法會呼叫 Object.Equals來判斷相等。
從 .NET Framework 2.0 開始,這個方法會使用 集合的物件 Equals 和 CompareTo 方法item
來判斷專案是否存在。 在舊版的 .NET Framework 中,已使用 Equals 集合中 物件的 和 CompareTo 方法item
,來判斷此判斷。