ArrayList.BinarySearch 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用二元搜尋演算法來定位排序中或其中一部分的特定元素 ArrayList 。
多載
| 名稱 | Description |
|---|---|
| BinarySearch(Object) |
使用預設比較子搜尋項目的整個排序 ArrayList,並傳回專案以零起始的索引。 |
| BinarySearch(Object, IComparer) |
使用指定的比較子搜尋整個已排序的 ArrayList 專案,並傳回專案以零起始的索引。 |
| BinarySearch(Int32, Int32, Object, IComparer) |
使用指定的比較子搜尋已排序之 ArrayList 中的專案範圍,並傳回專案以零起始的索引。 |
BinarySearch(Object)
使用預設比較子搜尋項目的整個排序 ArrayList,並傳回專案以零起始的索引。
public:
virtual int BinarySearch(System::Object ^ value);
public virtual int BinarySearch(object value);
abstract member BinarySearch : obj -> int
override this.BinarySearch : obj -> int
Public Overridable Function BinarySearch (value As Object) As Integer
參數
傳回
若 找到 ,則 在排序後 ArrayListvalue 中 的value零基索引;否則,為負數,即下一個大於 value 元素的指標的位元補數;若無較大元素,則為 的Count位元補數。
例外狀況
無論是 ElementsArrayList,都未value實作介面。IComparable
value 與 的 ArrayList元素類型不同。
範例
以下程式碼範例說明如何使用 BinarySearch 在 中定位特定物件 ArrayList。
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList. BinarySearch requires
// a sorted ArrayList.
ArrayList myAL = new ArrayList();
for ( int i = 0; i <= 4; i++ )
myAL.Add( i*2 );
// Displays the ArrayList.
Console.WriteLine( "The int ArrayList contains the following:" );
PrintValues( myAL );
// Locates a specific object that does not exist in the ArrayList.
Object myObjectOdd = 3;
FindMyObject( myAL, myObjectOdd );
// Locates an object that exists in the ArrayList.
Object myObjectEven = 6;
FindMyObject( myAL, myObjectEven );
}
public static void FindMyObject( ArrayList myList, Object myObject ) {
int myIndex=myList.BinarySearch( myObject );
if ( myIndex < 0 )
Console.WriteLine( "The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex );
else
Console.WriteLine( "The object to search for ({0}) is at index {1}.", myObject, myIndex );
}
public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
The int ArrayList contains the following:
0 2 4 6 8
The object to search for (3) is not found. The next larger object is at index 2.
The object to search for (6) is at index 3.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList. BinarySearch requires
' a sorted ArrayList.
Dim myAL As New ArrayList()
Dim i As Integer
For i = 0 To 4
myAL.Add(i * 2)
Next i
' Displays the ArrayList.
Console.WriteLine("The Int32 ArrayList contains the following:")
PrintValues(myAL)
' Locates a specific object that does not exist in the ArrayList.
Dim myObjectOdd As Object = 3
FindMyObject(myAL, myObjectOdd)
' Locates an object that exists in the ArrayList.
Dim myObjectEven As Object = 6
FindMyObject(myAL, myObjectEven)
End Sub
Public Shared Sub FindMyObject(myList As ArrayList, myObject As Object)
Dim myIndex As Integer = myList.BinarySearch(myObject)
If myIndex < 0 Then
Console.WriteLine("The object to search for ({0}) is not found. " _
+ "The next larger object is at index {1}.", myObject, _
Not myIndex)
Else
Console.WriteLine("The object to search for ({0}) is at index " _
+ "{1}.", myObject, myIndex)
End If
End Sub
Public Shared Sub PrintValues(myList As IEnumerable)
Dim obj As [Object]
For Each obj In myList
Console.Write(" {0}", obj)
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The Int32 ArrayList contains the following:
' 0 2 4 6 8
' The object to search for (3) is not found. The next larger object is at index 2.
' The object to search for (6) is at index 3.
備註
value參數與每個元素ArrayList必須實作IComparable介面,該介面用於比較。
ArrayList元素必須依實作定義IComparable的排序順序以遞增值排序;否則結果可能不正確。
允許與任意型別比較 null ,且在使用 IComparable時不會產生例外。 排序時, null 被視為比其他任何物件都小。
如果包含 ArrayList 多個相同值的元素,該方法只會回傳其中一次出現,且可能回傳任一次,不一定是第一個。
若 不 ArrayList 包含指定值,該方法回傳負整數。 你可以對這個負整數套用位元補數運算(~),得到第一個元素的索引大於搜尋值。 在將值 ArrayList插入 時,應使用此索引作為插入點以維持排序順序。
此方法是一個 O(log n) 運算,其中 n 為 Count。
另請參閱
適用於
BinarySearch(Object, IComparer)
使用指定的比較子搜尋整個已排序的 ArrayList 專案,並傳回專案以零起始的索引。
public:
virtual int BinarySearch(System::Object ^ value, System::Collections::IComparer ^ comparer);
public virtual int BinarySearch(object value, System.Collections.IComparer comparer);
abstract member BinarySearch : obj * System.Collections.IComparer -> int
override this.BinarySearch : obj * System.Collections.IComparer -> int
Public Overridable Function BinarySearch (value As Object, comparer As IComparer) As Integer
參數
傳回
若 找到 ,則 在排序後 ArrayListvalue 中 的value零基索引;否則,為負數,即下一個大於 value 元素的指標的位元補數;若無較大元素,則為 的Count位元補數。
例外狀況
comparer 是 null 且 也不是 value ,且 的 ArrayList 元素都未實作介面 IComparable 。
comparer 與 nullvalue 的元素類型相同,且 與 的元素 ArrayList類型相同。
範例
以下範例會產生一 ArrayList 組有色動物。 提供的 IComparer 會執行二分搜尋的字串比較。 同時會顯示迭代搜尋和二分搜尋的結果。
using System;
using System.Collections;
public class SimpleStringComparer : IComparer
{
int IComparer.Compare(object x, object y)
{
string cmpstr = (string)x;
return cmpstr.CompareTo((string)y);
}
}
public class MyArrayList : ArrayList
{
public static void Main()
{
// Creates and initializes a new ArrayList.
MyArrayList coloredAnimals = new MyArrayList();
coloredAnimals.Add("White Tiger");
coloredAnimals.Add("Pink Bunny");
coloredAnimals.Add("Red Dragon");
coloredAnimals.Add("Green Frog");
coloredAnimals.Add("Blue Whale");
coloredAnimals.Add("Black Cat");
coloredAnimals.Add("Yellow Lion");
// BinarySearch requires a sorted ArrayList.
coloredAnimals.Sort();
// Compare results of an iterative search with a binary search
int index = coloredAnimals.IterativeSearch("White Tiger");
Console.WriteLine("Iterative search, item found at index: {0}", index);
index = coloredAnimals.BinarySearch("White Tiger", new SimpleStringComparer());
Console.WriteLine("Binary search, item found at index: {0}", index);
}
public int IterativeSearch(object finditem)
{
int index = -1;
for (int i = 0; i < this.Count; i++)
{
if (finditem.Equals(this[i]))
{
index = i;
break;
}
}
return index;
}
}
//
// This code produces the following output.
//
// Iterative search, item found at index: 5
// Binary search, item found at index: 5
//
Imports System.Collections
Public Class SimpleStringComparer
Implements IComparer
Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
Dim cmpstr As String = CType(x, String)
Return cmpstr.CompareTo(CType(y, String))
End Function
End Class
Public Class MyArrayList
Inherits ArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim coloredAnimals As New MyArrayList()
coloredAnimals.Add("White Tiger")
coloredAnimals.Add("Pink Bunny")
coloredAnimals.Add("Red Dragon")
coloredAnimals.Add("Green Frog")
coloredAnimals.Add("Blue Whale")
coloredAnimals.Add("Black Cat")
coloredAnimals.Add("Yellow Lion")
' BinarySearch requires a sorted ArrayList.
coloredAnimals.Sort()
' Compare results of an iterative search with a binary search
Dim index As Integer = coloredAnimals.IterativeSearch("White Tiger")
Console.WriteLine("Iterative search, item found at index: {0}", index)
index = coloredAnimals.BinarySearch("White Tiger", New SimpleStringComparer())
Console.WriteLine("Binary search, item found at index: {0}", index)
End Sub
Public Function IterativeSearch(finditem As Object) As Integer
Dim index As Integer = -1
For i As Integer = 0 To MyClass.Count - 1
If finditem.Equals(MyClass.Item(i))
index = i
Exit For
End If
Next i
Return index
End Function
End Class
'
' This code produces the following output.
'
' Iterative search, item found at index: 5
' Binary search, item found at index: 5
'
備註
比較子會自定義項目的比較方式。 例如,你可以用一個 CaseInsensitiveComparer 實例作為比較器,執行大小寫不區分的字串搜尋。
若 comparer 提供 ,則將 中的 ArrayList 元素與指定值使用指定 IComparer 實作比較。
ArrayList元素必須依由 comparer定義的排序順序以遞增值排序;否則,結果可能會錯誤。
若 comparer 為 null,則使用 IComparable 元素本身提供的實作或指定值進行比較。
ArrayList元素必須依實作定義IComparable的排序順序以遞增值排序;否則結果可能不正確。
允許與任意型別比較 null ,且在使用 IComparable時不會產生例外。 排序時, null 被視為比其他任何物件都小。
如果包含 ArrayList 多個相同值的元素,該方法只會回傳其中一次出現,且可能回傳任一次,不一定是第一個。
若 不 ArrayList 包含指定值,該方法回傳負整數。 你可以對這個負整數套用位元補數運算(~),得到第一個元素的索引大於搜尋值。 在將值 ArrayList插入 時,應使用此索引作為插入點以維持排序順序。
此方法是一個 O(log n) 運算,其中 n 為 Count。
另請參閱
適用於
BinarySearch(Int32, Int32, Object, IComparer)
使用指定的比較子搜尋已排序之 ArrayList 中的專案範圍,並傳回專案以零起始的索引。
public:
virtual int BinarySearch(int index, int count, System::Object ^ value, System::Collections::IComparer ^ comparer);
public virtual int BinarySearch(int index, int count, object value, System.Collections.IComparer comparer);
abstract member BinarySearch : int * int * obj * System.Collections.IComparer -> int
override this.BinarySearch : int * int * obj * System.Collections.IComparer -> int
Public Overridable Function BinarySearch (index As Integer, count As Integer, value As Object, comparer As IComparer) As Integer
參數
- index
- Int32
搜尋範圍的零基起始索引。
- count
- Int32
要搜尋的範圍長度。
傳回
若 找到 ,則 在排序後 ArrayListvalue 中 的value零基索引;否則,為負數,即下一個大於 value 元素的指標的位元補數;若無較大元素,則為 的Count位元補數。
例外狀況
index 且 count 不表示 中的 ArrayList有效範圍。
-或-
comparer 是 null 且 也不是 value ,且 的 ArrayList 元素都未實作介面 IComparable 。
comparer 與 nullvalue 的元素類型相同,且 與 的元素 ArrayList類型相同。
備註
比較子會自定義項目的比較方式。 例如,你可以用一個 CaseInsensitiveComparer 實例作為比較器,執行大小寫不區分的字串搜尋。
若 comparer 提供 ,則將 中的 ArrayList 元素與指定值使用指定 IComparer 實作比較。
ArrayList元素必須依由 comparer定義的排序順序以遞增值排序;否則,結果可能會錯誤。
若 comparer 為 null,則使用 IComparable 元素本身提供的實作或指定值進行比較。
ArrayList元素必須依實作定義IComparable的排序順序以遞增值排序;否則結果可能不正確。
允許與任意型別比較 null ,且在使用 IComparable時不會產生例外。 排序時, null 被視為比其他任何物件都小。
如果包含 ArrayList 多個相同值的元素,該方法只會回傳其中一次出現,且可能回傳任一次,不一定是第一個。
若 不 ArrayList 包含指定值,該方法回傳負整數。 你可以對這個負整數套用位元補數運算(~),得到第一個元素的索引大於搜尋值。 在將值 ArrayList插入 時,應使用此索引作為插入點以維持排序順序。
此方法是一個 O(log n) 運算,其中 n 為 count。