ArrayList.BinarySearch 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用二进制搜索算法在排序 ArrayList 或其一部分中找到特定元素。
重载
BinarySearch(Object) |
使用默认比较器搜索整个排序 ArrayList 元素,并返回元素的从零开始的索引。 |
BinarySearch(Object, IComparer) |
使用指定的比较器搜索整个排序 ArrayList 元素,并返回该元素的从零开始的索引。 |
BinarySearch(Int32, Int32, Object, IComparer) |
使用指定的比较器在排序 ArrayList 中搜索元素的范围,并返回元素的从零开始的索引。 |
BinarySearch(Object)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
使用默认比较器搜索整个排序 ArrayList 元素,并返回元素的从零开始的索引。
public:
virtual int BinarySearch(System::Object ^ value);
public virtual int BinarySearch (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
参数
返回
如果找到 value
,则排序 ArrayList中 value
的从零开始的索引;否则为负数,即下一个大于 value
的元素的索引的按位补数;如果没有较大的元素,则为 Count的按位补数。
例外
value
和 ArrayList 的元素都不实现 IComparable 接口。
value
的类型与 ArrayList的元素不同。
示例
下面的代码示例演示如何使用 BinarySearch 在 ArrayList中查找特定对象。
using namespace System;
using namespace System::Collections;
void FindMyObject( ArrayList^ myList, Object^ myObject );
void PrintValues( IEnumerable^ myList );
int main()
{
// Creates and initializes a new ArrayList. BinarySearch requires
// a sorted ArrayList.
ArrayList^ myAL = gcnew ArrayList;
for ( int i = 0; i <= 4; i++ )
myAL->Add( i * 2 );
// Displays the ArrayList.
Console::WriteLine( "The Int32 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 );
}
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 );
}
void PrintValues( IEnumerable^ myList )
{
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::Write( " {0}", obj );
}
Console::WriteLine();
}
/*
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.
*/
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。
另请参阅
- 在集合 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch(Object, IComparer)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
使用指定的比较器搜索整个排序 ArrayList 元素,并返回该元素的从零开始的索引。
public:
virtual int BinarySearch(System::Object ^ value, System::Collections::IComparer ^ comparer);
public virtual int BinarySearch (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
参数
返回
如果找到 value
,则排序 ArrayList中 value
的从零开始的索引;否则为负数,即下一个大于 value
的元素的索引的按位补数;如果没有较大的元素,则为 Count的按位补数。
例外
comparer
是 null
的,value
和 ArrayList 的元素都不实现 IComparable 接口。
comparer
是 null
的,value
的类型与 ArrayList的元素不同。
示例
以下示例创建彩色动物 ArrayList。 提供的 IComparer 执行二进制搜索的字符串比较。 将显示迭代搜索和二进制搜索的结果。
using namespace System;
using namespace System::Collections;
public ref class SimpleStringComparer : public IComparer
{
virtual int Compare(Object^ x, Object^ y) sealed = IComparer::Compare
{
String^ cmpstr = (String^)x;
return cmpstr->CompareTo((String^)y);
}
};
public ref class MyArrayList : public ArrayList
{
public:
static void Main()
{
// Creates and initializes a new ArrayList.
MyArrayList^ coloredAnimals = gcnew 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", gcnew SimpleStringComparer());
Console::WriteLine("Binary search, item found at index: {0}", index);
}
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;
}
};
int main()
{
MyArrayList::Main();
}
//
// This code produces the following output.
//
// Iterative search, item found at index: 5
// Binary search, item found at index: 5
//
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
,则使用指定的 IComparer 实现将 ArrayList 的元素与指定值进行比较。
ArrayList 的元素必须已根据 comparer
定义的排序顺序按递增值进行排序;否则,结果可能不正确。
如果 comparer
null
,则使用元素本身或指定值提供的 IComparable 实现完成比较。
ArrayList 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
将 null
与任何类型进行比较,在使用 IComparable时不会生成异常。 排序时,null
被视为小于任何其他对象。
如果 ArrayList 包含多个具有相同值的元素,该方法只返回其中一个匹配项,并且可能会返回任何一个匹配项,不一定返回第一个。
如果 ArrayList 不包含指定值,该方法将返回负整数。 可以将按位补运算 (~) 应用于此负整数,以获取大于搜索值的第一个元素的索引。 将值插入 ArrayList时,此索引应用作插入点来维护排序顺序。
此方法是 O(log n)
操作,其中 n
Count。
另请参阅
- 在集合 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch(Int32, Int32, Object, IComparer)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
使用指定的比较器在排序 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);
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
要搜索的范围的长度。
返回
如果找到 value
,则排序 ArrayList中 value
的从零开始的索引;否则为负数,即下一个大于 value
的元素的索引的按位补数;如果没有较大的元素,则为 Count的按位补数。
例外
comparer
是 null
的,value
的类型与 ArrayList的元素不同。
注解
比较器自定义元素的比较方式。 例如,可以使用 CaseInsensitiveComparer 实例作为比较器来执行不区分大小写的字符串搜索。
如果提供了 comparer
,则使用指定的 IComparer 实现将 ArrayList 的元素与指定值进行比较。
ArrayList 的元素必须已根据 comparer
定义的排序顺序按递增值进行排序;否则,结果可能不正确。
如果 comparer
null
,则使用元素本身或指定值提供的 IComparable 实现完成比较。
ArrayList 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
将 null
与任何类型进行比较,在使用 IComparable时不会生成异常。 排序时,null
被视为小于任何其他对象。
如果 ArrayList 包含多个具有相同值的元素,该方法只返回其中一个匹配项,并且可能会返回任何一个匹配项,不一定返回第一个。
如果 ArrayList 不包含指定值,该方法将返回负整数。 可以将按位补运算 (~) 应用于此负整数,以获取大于搜索值的第一个元素的索引。 将值插入 ArrayList时,此索引应用作插入点来维护排序顺序。
此方法是 O(log n)
操作,其中 n
count
。
另请参阅
- IComparer
- IComparable
- 在集合 中执行 Culture-Insensitive 字符串操作