ArrayList.BinarySearch 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用二进制搜索算法在排序 ArrayList 或部分元素中查找特定元素。
重载
| 名称 | 说明 |
|---|---|
| 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数。
例外
既value不是实现接口的元素,也不是实现接口的ArrayListIComparable元素。
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实现接口的ArrayListIComparable元素。
comparer
value与null元素的类型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实现接口的ArrayListIComparable元素。
comparer
value与null元素的类型ArrayList不同。
注解
比较器自定义元素的比较方式。 例如,可以使用 CaseInsensitiveComparer 实例作为比较器来执行不区分大小写的字符串搜索。
如果 comparer 提供,则 ArrayList 使用指定的实现将元素与指定 IComparer 值进行比较。
ArrayList必须已根据定义的comparer排序顺序按递增值对元素进行排序;否则,结果可能不正确。
comparer如果是null,则使用IComparable元素本身或指定值提供的实现完成比较。
ArrayList必须已根据实现定义的IComparable排序顺序按递增值对元素进行排序;否则,结果可能不正确。
与 null 任何类型进行比较是允许的,在使用时 IComparable不会生成异常。 排序时, null 被视为小于任何其他对象。
ArrayList如果包含具有相同值的多个元素,该方法只返回其中一个匹配项,并且可能会返回任何一个匹配项,不一定返回第一个。
ArrayList如果不包含指定值,该方法将返回负整数。 可以将按位补运算 (~) 应用于此负整数,以获取大于搜索值的第一个元素的索引。 将值 ArrayList插入到该索引中时,此索引应用作插入点来维护排序顺序。
此方法是一个O(log n)操作,其中 n 。count