Array.BinarySearch 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用二进制搜索算法搜索一维排序 Array 值。
重载
| BinarySearch(Array, Object) |
使用数组的每个元素和指定对象实现的 IComparable 接口,搜索整个一维排序数组中的特定元素。 |
| BinarySearch(Array, Object, IComparer) |
使用指定的 IComparer 接口搜索整个一维排序数组中的值。 |
| BinarySearch(Array, Int32, Int32, Object) |
使用数组的每个元素和指定值实现的 IComparable 接口,在一维排序数组中搜索一系列元素。 |
| BinarySearch(Array, Int32, Int32, Object, IComparer) |
使用指定的 IComparer 接口搜索一维排序数组中的元素范围以获取值。 |
| BinarySearch<T>(T[], T) |
使用由 Array 的每个元素和指定对象实现的 IComparable<T> 泛型接口,搜索整个一维排序数组中的特定元素。 |
| BinarySearch<T>(T[], T, IComparer<T>) |
使用指定的 IComparer<T> 泛型接口搜索整个一维排序数组中的值。 |
| BinarySearch<T>(T[], Int32, Int32, T) |
使用由 Array 的每个元素和指定值实现的 IComparable<T> 泛型接口,在一维排序数组中的元素范围中搜索值。 |
| BinarySearch<T>(T[], Int32, Int32, T, IComparer<T>) |
使用指定的 IComparer<T> 泛型接口在一维排序数组中搜索值的范围。 |
BinarySearch(Array, Object)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用数组的每个元素和指定对象实现的 IComparable 接口,搜索整个一维排序数组中的特定元素。
public:
static int BinarySearch(Array ^ array, System::Object ^ value);
public static int BinarySearch (Array array, object value);
public static int BinarySearch (Array array, object? value);
static member BinarySearch : Array * obj -> int
Public Shared Function BinarySearch (array As Array, value As Object) As Integer
参数
- value
- Object
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
array 是多维的。
value 的类型与 array元素不兼容。
value 不实现 IComparable 接口,搜索遇到不实现 IComparable 接口的元素。
示例
下面的代码示例演示如何使用 BinarySearch 在 Array中查找特定对象。
注意
该数组的元素按升序排序创建。 BinarySearch 方法要求按升序对数组进行排序。
using namespace System;
public ref class SamplesArray
{
public:
static void Main()
{
// Creates and initializes a new Array.
Array^ myIntArray = Array::CreateInstance(Int32::typeid, 5);
myIntArray->SetValue(8, 0);
myIntArray->SetValue(2, 1);
myIntArray->SetValue(6, 2);
myIntArray->SetValue(3, 3);
myIntArray->SetValue(7, 4);
// Do the required sort first
Array::Sort(myIntArray);
// Displays the values of the Array.
Console::WriteLine("The Int32 array contains the following:");
PrintValues(myIntArray);
// Locates a specific object that does not exist in the Array.
Object^ myObjectOdd = 1;
FindMyObject(myIntArray, myObjectOdd);
// Locates an object that exists in the Array.
Object^ myObjectEven = 6;
FindMyObject(myIntArray, myObjectEven);
}
static void FindMyObject(Array^ myArr, Object^ myObject)
{
int myIndex = Array::BinarySearch(myArr, 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);
}
}
static void PrintValues(Array^ myArr)
{
int i = 0;
int cols = myArr->GetLength(myArr->Rank - 1);
for each (Object^ o in myArr)
{
if ( i < cols )
{
i++;
}
else
{
Console::WriteLine();
i = 1;
}
Console::Write("\t{0}", o);
}
Console::WriteLine();
}
};
int main()
{
SamplesArray::Main();
}
// This code produces the following output.
//
//The Int32 array contains the following:
// 2 3 6 7 8
//The object to search for (1) is not found. The next larger object is at index 0
//
//The object to search for (6) is at index 2.
open System
let printValues (myArray: Array) =
let mutable i = 0
let cols = myArray.GetLength(myArray.Rank - 1)
for item in myArray do
if i < cols then
i <- i + 1
else
printfn ""
i <- 1;
printf $"\t{item}"
printfn ""
let findMyObject (myArr: Array) (myObject: obj) =
let myIndex = Array.BinarySearch(myArr, myObject)
if myIndex < 0 then
printfn $"The object to search for ({myObject}) is not found. The next larger object is at index {~~~myIndex}."
else
printfn $"The object to search for ({myObject}) is at index {myIndex}."
// Creates and initializes a new Array.
let myIntArray = [| 8; 2; 6; 3; 7 |]
// Do the required sort first
Array.Sort myIntArray
// Displays the values of the Array.
printfn "The int array contains the following:"
printValues myIntArray
// Locates a specific object that does not exist in the Array.
let myObjectOdd: obj = 1
findMyObject myIntArray myObjectOdd
// Locates an object that exists in the Array.
let myObjectEven: obj = 6
findMyObject myIntArray myObjectEven
// This code produces the following output:
// The int array contains the following:
// 2 3 6 7 8
// The object to search for (1) is not found. The next larger object is at index 0.
// The object to search for (6) is at index 2.
using System;
public class SamplesArray
{
public static void Main()
{
// Creates and initializes a new Array.
Array myIntArray = Array.CreateInstance(typeof(int), 5);
myIntArray.SetValue(8, 0);
myIntArray.SetValue(2, 1);
myIntArray.SetValue(6, 2);
myIntArray.SetValue(3, 3);
myIntArray.SetValue(7, 4);
// Do the required sort first
Array.Sort(myIntArray);
// Displays the values of the Array.
Console.WriteLine( "The int array contains the following:" );
PrintValues(myIntArray);
// Locates a specific object that does not exist in the Array.
object myObjectOdd = 1;
FindMyObject( myIntArray, myObjectOdd );
// Locates an object that exists in the Array.
object myObjectEven = 6;
FindMyObject(myIntArray, myObjectEven);
}
public static void FindMyObject(Array myArr, object myObject)
{
int myIndex=Array.BinarySearch(myArr, 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(Array myArr)
{
int i = 0;
int cols = myArr.GetLength(myArr.Rank - 1);
foreach (object o in myArr)
{
if ( i < cols )
{
i++;
}
else
{
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", o);
}
Console.WriteLine();
}
}
// This code produces the following output.
//
//The int array contains the following:
// 2 3 6 7 8
//The object to search for (1) is not found. The next larger object is at index 0
//
//The object to search for (6) is at index 2.
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array.
Dim myIntArray As Array = Array.CreateInstance( GetType(Int32), 5 )
myIntArray.SetValue( 8, 0 )
myIntArray.SetValue( 2, 1 )
myIntArray.SetValue( 6, 2 )
myIntArray.SetValue( 3, 3 )
myIntArray.SetValue( 7, 4 )
' Do the required sort first
Array.Sort(myIntArray)
' Displays the values of the Array.
Console.WriteLine("The Int32 array contains the following:")
PrintValues(myIntArray)
' Locates a specific object that does not exist in the Array.
Dim myObjectOdd As Object = 1
FindMyObject(myIntArray, myObjectOdd)
' Locates an object that exists in the Array.
Dim myObjectEven As Object = 6
FindMyObject(myIntArray, myObjectEven)
End Sub
Public Shared Sub FindMyObject(myArr As Array, myObject As Object)
Dim myIndex As Integer = Array.BinarySearch(myArr, 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(myArr As Array)
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength( myArr.Rank - 1 )
For Each o As Object In myArr
If i < cols Then
i += 1
Else
Console.WriteLine()
i = 1
End If
Console.Write( vbTab + "{0}", o)
Next o
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The Int32 array contains the following:
' 2 3 6 7 8
' The object to search for (1) is not found. The next larger object is at index 0
'
' The object to search for (6) is at index 2.
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果 Array 不包含指定值,该方法将返回负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引大于数组的上限,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
value 或 array 的每个元素都必须实现用于比较的 IComparable 接口。
array 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
注意
如果value 未实现 IComparable 接口,则搜索开始前不会测试 array 的元素 IComparable。 如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable 实现。 也就是说,IComparable 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 n 是 array的 Length。
另请参阅
- IComparable
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch(Array, Object, IComparer)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用指定的 IComparer 接口搜索整个一维排序数组中的值。
public:
static int BinarySearch(Array ^ array, System::Object ^ value, System::Collections::IComparer ^ comparer);
public static int BinarySearch (Array array, object value, System.Collections.IComparer comparer);
public static int BinarySearch (Array array, object? value, System.Collections.IComparer? comparer);
static member BinarySearch : Array * obj * System.Collections.IComparer -> int
Public Shared Function BinarySearch (array As Array, value As Object, comparer As IComparer) As Integer
参数
- value
- Object
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
array 是多维的。
comparer 是 null,value 的类型与 array元素不兼容。
comparer 是 null,value 不实现 IComparable 接口,搜索遇到不实现 IComparable 接口的元素。
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果 Array 不包含指定值,该方法将返回负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引大于数组的上限,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
比较器自定义元素的比较方式。 例如,可以使用 System.Collections.CaseInsensitiveComparer 作为比较器来执行不区分大小写的字符串搜索。
如果未 nullcomparer,则使用指定的 IComparer 实现将 array 的元素与指定值进行比较。
array 的元素必须已根据 comparer定义的排序顺序按递增值进行排序;否则,结果可能不正确。
如果comparernull,则使用元素本身或指定值提供的 IComparable 实现完成比较。
array 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
注意
如果 comparernull 且 value 未实现 IComparable 接口,则 array 元素在搜索开始前不会针对 IComparable 进行测试。 如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable 实现。 也就是说,IComparable 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 n 是 array的 Length。
另请参阅
- IComparer
- IComparable
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch(Array, Int32, Int32, Object)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用数组的每个元素和指定值实现的 IComparable 接口,在一维排序数组中搜索一系列元素。
public:
static int BinarySearch(Array ^ array, int index, int length, System::Object ^ value);
public static int BinarySearch (Array array, int index, int length, object value);
public static int BinarySearch (Array array, int index, int length, object? value);
static member BinarySearch : Array * int * int * obj -> int
Public Shared Function BinarySearch (array As Array, index As Integer, length As Integer, value As Object) As Integer
参数
- index
- Int32
要搜索的范围的起始索引。
- length
- Int32
要搜索的范围的长度。
- value
- Object
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
array 是多维的。
value 不实现 IComparable 接口,搜索遇到不实现 IComparable 接口的元素。
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果 Array 不包含指定值,该方法将返回负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引大于数组的上限,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
value 或 array 的每个元素都必须实现用于比较的 IComparable 接口。
array 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
注意
如果 value 未实现 IComparable 接口,则搜索开始前不会测试 array 的元素 IComparable。 如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable 实现。 也就是说,IComparable 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 nlength。
另请参阅
- IComparable
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch(Array, Int32, Int32, Object, IComparer)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用指定的 IComparer 接口搜索一维排序数组中的元素范围以获取值。
public:
static int BinarySearch(Array ^ array, int index, int length, System::Object ^ value, System::Collections::IComparer ^ comparer);
public static int BinarySearch (Array array, int index, int length, object value, System.Collections.IComparer comparer);
public static int BinarySearch (Array array, int index, int length, object? value, System.Collections.IComparer? comparer);
static member BinarySearch : Array * int * int * obj * System.Collections.IComparer -> int
Public Shared Function BinarySearch (array As Array, index As Integer, length As Integer, value As Object, comparer As IComparer) As Integer
参数
- index
- Int32
要搜索的范围的起始索引。
- length
- Int32
要搜索的范围的长度。
- value
- Object
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
array 是多维的。
comparer 是 null,value 不实现 IComparable 接口,搜索遇到不实现 IComparable 接口的元素。
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果 Array 不包含指定值,该方法将返回负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引大于数组的上限,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
比较器自定义元素的比较方式。 例如,可以使用 System.Collections.CaseInsensitiveComparer 作为比较器来执行不区分大小写的字符串搜索。
如果未 nullcomparer,则使用指定的 IComparer 实现将 array 的元素与指定值进行比较。
array 的元素必须已根据 comparer定义的排序顺序按递增值进行排序;否则,结果可能不正确。
如果 comparernull,则使用元素本身或指定值提供的 IComparable 实现完成比较。
array 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
注意
如果 comparernull 且 value 未实现 IComparable 接口,则 array 元素在搜索开始前不会针对 IComparable 进行测试。 如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,使用 IComparable时,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable 实现。 也就是说,IComparable 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 nlength。
另请参阅
- IComparer
- IComparable
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch<T>(T[], T)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用由 Array 的每个元素和指定对象实现的 IComparable<T> 泛型接口,搜索整个一维排序数组中的特定元素。
public:
generic <typename T>
static int BinarySearch(cli::array <T> ^ array, T value);
public static int BinarySearch<T> (T[] array, T value);
static member BinarySearch : 'T[] * 'T -> int
Public Shared Function BinarySearch(Of T) (array As T(), value As T) As Integer
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的一维、从零开始的 Array 排序。
- value
- T
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
T 不实现 IComparable<T> 泛型接口。
示例
下面的代码示例演示 Sort<T>(T[]) 泛型方法重载和 BinarySearch<T>(T[], T) 泛型方法重载。 不按特定顺序创建字符串数组。
该数组将再次显示、排序和显示。 必须对数组进行排序才能使用 BinarySearch 方法。
注意
对 Sort 和 BinarySearch 泛型方法的调用与对其非泛型方法的调用不同,因为 Visual Basic、F#、C# 和C++从第一个参数的类型推断泛型类型参数的类型。 如果使用 Ildasm.exe(IL 反汇编程序) 检查Microsoft中间语言(MSIL),可以看到正在调用泛型方法。
然后,BinarySearch<T>(T[], T) 泛型方法重载用于搜索两个字符串,一个字符串不在数组中,一个不在数组中。
BinarySearch 方法的数组和返回值将传递给 ShowWhere 泛型方法(F# 示例中的 showWhere 函数),如果找到字符串,则显示索引值;否则,搜索字符串在数组中时将介于其之间的元素。 如果字符串不在数组中,则索引为负值,因此 ShowWhere 方法采用按位补号(C# 和 Visual C++ 中的 ~ 运算符,在 Visual Basic 中为 f# 中 , Xor-1) 获取列表中大于搜索字符串的第一个元素的索引。
using namespace System;
using namespace System::Collections::Generic;
generic<typename T> void ShowWhere(array<T>^ arr, int index)
{
if (index<0)
{
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
//
index = ~index;
Console::Write("Not found. Sorts between: ");
if (index == 0)
Console::Write("beginning of array and ");
else
Console::Write("{0} and ", arr[index-1]);
if (index == arr->Length)
Console::WriteLine("end of array.");
else
Console::WriteLine("{0}.", arr[index]);
}
else
{
Console::WriteLine("Found at index {0}.", index);
}
};
void main()
{
array<String^>^ dinosaurs = {"Pachycephalosaurus",
"Amargasaurus",
"Tyrannosaurus",
"Mamenchisaurus",
"Deinonychus",
"Edmontosaurus"};
Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nSort");
Array::Sort(dinosaurs);
Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nBinarySearch for 'Coelophysis':");
int index = Array::BinarySearch(dinosaurs, "Coelophysis");
ShowWhere(dinosaurs, index);
Console::WriteLine("\nBinarySearch for 'Tyrannosaurus':");
index = Array::BinarySearch(dinosaurs, "Tyrannosaurus");
ShowWhere(dinosaurs, index);
}
/* This code example produces the following output:
Pachycephalosaurus
Amargasaurus
Tyrannosaurus
Mamenchisaurus
Deinonychus
Edmontosaurus
Sort
Amargasaurus
Deinonychus
Edmontosaurus
Mamenchisaurus
Pachycephalosaurus
Tyrannosaurus
BinarySearch for 'Coelophysis':
Not found. Sorts between: Amargasaurus and Deinonychus.
BinarySearch for 'Tyrannosaurus':
Found at index 5.
*/
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
string[] dinosaurs = {"Pachycephalosaurus",
"Amargasaurus",
"Tyrannosaurus",
"Mamenchisaurus",
"Deinonychus",
"Edmontosaurus"};
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nSort");
Array.Sort(dinosaurs);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch for 'Coelophysis':");
int index = Array.BinarySearch(dinosaurs, "Coelophysis");
ShowWhere(dinosaurs, index);
Console.WriteLine("\nBinarySearch for 'Tyrannosaurus':");
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus");
ShowWhere(dinosaurs, index);
}
private static void ShowWhere<T>(T[] array, int index)
{
if (index<0)
{
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
//
index = ~index;
Console.Write("Not found. Sorts between: ");
if (index == 0)
Console.Write("beginning of array and ");
else
Console.Write("{0} and ", array[index-1]);
if (index == array.Length)
Console.WriteLine("end of array.");
else
Console.WriteLine("{0}.", array[index]);
}
else
{
Console.WriteLine("Found at index {0}.", index);
}
}
}
/* This code example produces the following output:
Pachycephalosaurus
Amargasaurus
Tyrannosaurus
Mamenchisaurus
Deinonychus
Edmontosaurus
Sort
Amargasaurus
Deinonychus
Edmontosaurus
Mamenchisaurus
Pachycephalosaurus
Tyrannosaurus
BinarySearch for 'Coelophysis':
Not found. Sorts between: Amargasaurus and Deinonychus.
BinarySearch for 'Tyrannosaurus':
Found at index 5.
*/
open System
let showWhere (array: 'a []) index =
if index < 0 then
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
let index = ~~~index
printf "Not found. Sorts between: "
if index = 0 then
printf "beginning of array and "
else
printf $"{array[index - 1]} and "
if index = array.Length then
printfn "end of array."
else
printfn $"{array[index]}."
else
printfn $"Found at index {index}."
let dinosaurs =
[| "Pachycephalosaurus"
"Amargasaurus"
"Tyrannosaurus"
"Mamenchisaurus"
"Deinonychus"
"Edmontosaurus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
printfn "\nSort"
Array.Sort dinosaurs
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
printfn "\nBinarySearch for 'Coelophysis':"
let index = Array.BinarySearch(dinosaurs, "Coelophysis")
showWhere dinosaurs index
printfn "\nBinarySearch for 'Tyrannosaurus':"
Array.BinarySearch(dinosaurs, "Tyrannosaurus")
|> showWhere dinosaurs
// This code example produces the following output:
//
// Pachycephalosaurus
// Amargasaurus
// Tyrannosaurus
// Mamenchisaurus
// Deinonychus
// Edmontosaurus
//
// Sort
//
// Amargasaurus
// Deinonychus
// Edmontosaurus
// Mamenchisaurus
// Pachycephalosaurus
// Tyrannosaurus
//
// BinarySearch for 'Coelophysis':
// Not found. Sorts between: Amargasaurus and Deinonychus.
//
// BinarySearch for 'Tyrannosaurus':
// Found at index 5.
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { _
"Pachycephalosaurus", _
"Amargasaurus", _
"Tyrannosaurus", _
"Mamenchisaurus", _
"Deinonychus", _
"Edmontosaurus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "Sort")
Array.Sort(dinosaurs)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"BinarySearch for 'Coelophysis':")
Dim index As Integer = _
Array.BinarySearch(dinosaurs, "Coelophysis")
ShowWhere(dinosaurs, index)
Console.WriteLine(vbLf & _
"BinarySearch for 'Tyrannosaurus':")
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus")
ShowWhere(dinosaurs, index)
End Sub
Private Shared Sub ShowWhere(Of T) _
(ByVal array() As T, ByVal index As Integer)
If index < 0 Then
' If the index is negative, it represents the bitwise
' complement of the next larger element in the array.
'
index = index Xor -1
Console.Write("Not found. Sorts between: ")
If index = 0 Then
Console.Write("beginning of array and ")
Else
Console.Write("{0} and ", array(index - 1))
End If
If index = array.Length Then
Console.WriteLine("end of array.")
Else
Console.WriteLine("{0}.", array(index))
End If
Else
Console.WriteLine("Found at index {0}.", index)
End If
End Sub
End Class
' This code example produces the following output:
'
'Pachycephalosaurus
'Amargasaurus
'Tyrannosaurus
'Mamenchisaurus
'Deinonychus
'Edmontosaurus
'
'Sort
'
'Amargasaurus
'Deinonychus
'Edmontosaurus
'Mamenchisaurus
'Pachycephalosaurus
'Tyrannosaurus
'
'BinarySearch for 'Coelophysis':
'Not found. Sorts between: Amargasaurus and Deinonychus.
'
'BinarySearch for 'Tyrannosaurus':
'Found at index 5.
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果 array 不包含指定的值,该方法将返回负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引等于数组的大小,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
T 必须实现用于比较的 IComparable<T> 泛型接口。
array 的元素必须已根据 IComparable<T> 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable<T> 实现。 也就是说,IComparable<T> 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 n 是 array的 Length。
另请参阅
- IComparable<T>
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch<T>(T[], T, IComparer<T>)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用指定的 IComparer<T> 泛型接口搜索整个一维排序数组中的值。
public:
generic <typename T>
static int BinarySearch(cli::array <T> ^ array, T value, System::Collections::Generic::IComparer<T> ^ comparer);
public static int BinarySearch<T> (T[] array, T value, System.Collections.Generic.IComparer<T> comparer);
public static int BinarySearch<T> (T[] array, T value, System.Collections.Generic.IComparer<T>? comparer);
static member BinarySearch : 'T[] * 'T * System.Collections.Generic.IComparer<'T> -> int
Public Shared Function BinarySearch(Of T) (array As T(), value As T, comparer As IComparer(Of T)) As Integer
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的一维、从零开始的 Array 排序。
- value
- T
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
comparer 是 null,value 的类型与 array元素不兼容。
comparer
null,T 不实现 IComparable<T> 泛型接口
示例
以下示例演示泛型方法重载和 BinarySearch<T>(T[], T, IComparer<T>) 泛型方法重载 Sort<T>(T[], IComparer<T>)。
该代码示例为名为 ReverseCompare的字符串定义一个替代比较器,该比较器实现 visual Basic 中的 IComparer<string>(IComparer(Of String),在 Visual C++) 泛型接口中 IComparer<String^>。 比较器调用 CompareTo(String) 方法,反转比较器的顺序,使字符串排序高到低,而不是低到高。
该数组将再次显示、排序和显示。 必须对数组进行排序才能使用 BinarySearch 方法。
注意
对 Sort<T>(T[], IComparer<T>) 和 BinarySearch<T>(T[], T, IComparer<T>) 泛型方法的调用与其非泛型方法的调用不同,因为 Visual Basic、C# 和C++从第一个参数的类型推断泛型类型参数的类型。 如果使用 Ildasm.exe(IL 反汇编程序) 检查Microsoft中间语言(MSIL),可以看到正在调用泛型方法。
然后,BinarySearch<T>(T[], T, IComparer<T>) 泛型方法重载用于搜索两个字符串,一个字符串不在数组中,一个不在数组中。
BinarySearch<T>(T[], T, IComparer<T>) 方法的数组和返回值将传递给 ShowWhere 泛型方法(F# 示例中的 showWhere 函数),如果找到字符串,则显示索引值;否则,搜索字符串在数组中时将介于其之间的元素。 如果字符串不是数组的 n,则索引为负值,因此 ShowWhere 方法采用按位补号(C# 和 Visual C++ 中的 ~ 运算符,在 Visual Basic 中 Xor -1 ) 获取列表中大于搜索字符串的第一个元素的索引。
using namespace System;
using namespace System::Collections::Generic;
public ref class ReverseComparer: IComparer<String^>
{
public:
virtual int Compare(String^ x, String^ y)
{
// Compare y and x in reverse order.
return y->CompareTo(x);
}
};
generic<typename T> void ShowWhere(array<T>^ arr, int index)
{
if (index<0)
{
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
//
index = ~index;
Console::Write("Not found. Sorts between: ");
if (index == 0)
Console::Write("beginning of array and ");
else
Console::Write("{0} and ", arr[index-1]);
if (index == arr->Length)
Console::WriteLine("end of array.");
else
Console::WriteLine("{0}.", arr[index]);
}
else
{
Console::WriteLine("Found at index {0}.", index);
}
};
void main()
{
array<String^>^ dinosaurs = {"Pachycephalosaurus",
"Amargasaurus",
"Tyrannosaurus",
"Mamenchisaurus",
"Deinonychus",
"Edmontosaurus"};
Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
ReverseComparer^ rc = gcnew ReverseComparer();
Console::WriteLine("\nSort");
Array::Sort(dinosaurs, rc);
Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nBinarySearch for 'Coelophysis':");
int index = Array::BinarySearch(dinosaurs, "Coelophysis", rc);
ShowWhere(dinosaurs, index);
Console::WriteLine("\nBinarySearch for 'Tyrannosaurus':");
index = Array::BinarySearch(dinosaurs, "Tyrannosaurus", rc);
ShowWhere(dinosaurs, index);
}
/* This code example produces the following output:
Pachycephalosaurus
Amargasaurus
Tyrannosaurus
Mamenchisaurus
Deinonychus
Edmontosaurus
Sort
Tyrannosaurus
Pachycephalosaurus
Mamenchisaurus
Edmontosaurus
Deinonychus
Amargasaurus
BinarySearch for 'Coelophysis':
Not found. Sorts between: Deinonychus and Amargasaurus.
BinarySearch for 'Tyrannosaurus':
Found at index 0.
*/
using System;
using System.Collections.Generic;
public class ReverseComparer: IComparer<string>
{
public int Compare(string x, string y)
{
// Compare y and x in reverse order.
return y.CompareTo(x);
}
}
public class Example
{
public static void Main()
{
string[] dinosaurs = {"Pachycephalosaurus",
"Amargasaurus",
"Tyrannosaurus",
"Mamenchisaurus",
"Deinonychus",
"Edmontosaurus"};
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
ReverseComparer rc = new ReverseComparer();
Console.WriteLine("\nSort");
Array.Sort(dinosaurs, rc);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch for 'Coelophysis':");
int index = Array.BinarySearch(dinosaurs, "Coelophysis", rc);
ShowWhere(dinosaurs, index);
Console.WriteLine("\nBinarySearch for 'Tyrannosaurus':");
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus", rc);
ShowWhere(dinosaurs, index);
}
private static void ShowWhere<T>(T[] array, int index)
{
if (index<0)
{
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
//
index = ~index;
Console.Write("Not found. Sorts between: ");
if (index == 0)
Console.Write("beginning of array and ");
else
Console.Write("{0} and ", array[index-1]);
if (index == array.Length)
Console.WriteLine("end of array.");
else
Console.WriteLine("{0}.", array[index]);
}
else
{
Console.WriteLine("Found at index {0}.", index);
}
}
}
/* This code example produces the following output:
Pachycephalosaurus
Amargasaurus
Tyrannosaurus
Mamenchisaurus
Deinonychus
Edmontosaurus
Sort
Tyrannosaurus
Pachycephalosaurus
Mamenchisaurus
Edmontosaurus
Deinonychus
Amargasaurus
BinarySearch for 'Coelophysis':
Not found. Sorts between: Deinonychus and Amargasaurus.
BinarySearch for 'Tyrannosaurus':
Found at index 0.
*/
open System
open System.Collections.Generic
type ReverseComparer() =
interface IComparer<string> with
member _.Compare(x, y) =
// Compare y and x in reverse order.
y.CompareTo x
let showWhere (array: 'a []) index =
if index < 0 then
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
let index = ~~~index
printf "Not found. Sorts between: "
if index = 0 then
printf "beginning of array and "
else
printf $"{array[index - 1]} and "
if index = array.Length then
printfn "end of array."
else
printfn $"{array[index]}."
else
printfn $"Found at index {index}."
let dinosaurs =
[| "Pachycephalosaurus"
"Amargasaurus"
"Tyrannosaurus"
"Mamenchisaurus"
"Deinonychus"
"Edmontosaurus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
let rc = ReverseComparer()
printfn "\nSort"
Array.Sort(dinosaurs, rc)
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
printfn "\nBinarySearch for 'Coelophysis':"
Array.BinarySearch(dinosaurs, "Coelophysis", rc)
|> showWhere dinosaurs
printfn "\nBinarySearch for 'Tyrannosaurus':"
Array.BinarySearch(dinosaurs, "Tyrannosaurus", rc)
|> showWhere dinosaurs
// This code example produces the following output:
// Pachycephalosaurus
// Amargasaurus
// Tyrannosaurus
// Mamenchisaurus
// Deinonychus
// Edmontosaurus
//
// Sort
//
// Tyrannosaurus
// Pachycephalosaurus
// Mamenchisaurus
// Edmontosaurus
// Deinonychus
// Amargasaurus
//
// BinarySearch for 'Coelophysis':
// Not found. Sorts between: Deinonychus and Amargasaurus.
//
// BinarySearch for 'Tyrannosaurus':
// Found at index 0.
Imports System.Collections.Generic
Public Class ReverseComparer
Implements IComparer(Of String)
Public Function Compare(ByVal x As String, _
ByVal y As String) As Integer _
Implements IComparer(Of String).Compare
' Compare y and x in reverse order.
Return y.CompareTo(x)
End Function
End Class
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { _
"Pachycephalosaurus", _
"Amargasaurus", _
"Tyrannosaurus", _
"Mamenchisaurus", _
"Deinonychus", _
"Edmontosaurus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Dim rc As New ReverseComparer()
Console.WriteLine(vbLf & "Sort")
Array.Sort(dinosaurs, rc)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"BinarySearch for 'Coelophysis':")
Dim index As Integer = _
Array.BinarySearch(dinosaurs, "Coelophysis", rc)
ShowWhere(dinosaurs, index)
Console.WriteLine(vbLf & _
"BinarySearch for 'Tyrannosaurus':")
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus", rc)
ShowWhere(dinosaurs, index)
End Sub
Private Shared Sub ShowWhere(Of T) _
(ByVal array() As T, ByVal index As Integer)
If index < 0 Then
' If the index is negative, it represents the bitwise
' complement of the next larger element in the array.
'
index = index Xor -1
Console.Write("Not found. Sorts between: ")
If index = 0 Then
Console.Write("beginning of array and ")
Else
Console.Write("{0} and ", array(index - 1))
End If
If index = array.Length Then
Console.WriteLine("end of array.")
Else
Console.WriteLine("{0}.", array(index))
End If
Else
Console.WriteLine("Found at index {0}.", index)
End If
End Sub
End Class
' This code example produces the following output:
'
'Pachycephalosaurus
'Amargasaurus
'Tyrannosaurus
'Mamenchisaurus
'Deinonychus
'Edmontosaurus
'
'Sort
'
'Tyrannosaurus
'Pachycephalosaurus
'Mamenchisaurus
'Edmontosaurus
'Deinonychus
'Amargasaurus
'
'BinarySearch for 'Coelophysis':
'Not found. Sorts between: Deinonychus and Amargasaurus.
'
'BinarySearch for 'Tyrannosaurus':
'Found at index 0.
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果 Array 不包含指定值,该方法将返回负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引等于数组的大小,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
比较器自定义元素的比较方式。 例如,可以使用 System.Collections.CaseInsensitiveComparer 作为比较器来执行不区分大小写的字符串搜索。
如果未 nullcomparer,则使用指定的 IComparer<T> 泛型接口实现将 array 的元素与指定的值进行比较。
array 的元素必须已根据 comparer定义的排序顺序按递增值进行排序;否则,结果可能不正确。
如果 comparernull,则使用 T提供的 IComparable<T> 泛型接口实现完成比较。
array 的元素必须已根据 IComparable<T> 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
注意
如果 comparernull 且 value 未实现 IComparable<T> 泛型接口,则搜索开始前不会针对 IComparable<T> 测试 array 的元素。 如果搜索遇到未实现 IComparable<T>的元素,则会引发异常。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable<T> 实现。 也就是说,IComparable<T> 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 n 是 array的 Length。
另请参阅
- IComparer<T>
- IComparable<T>
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch<T>(T[], Int32, Int32, T)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用由 Array 的每个元素和指定值实现的 IComparable<T> 泛型接口,在一维排序数组中的元素范围中搜索值。
public:
generic <typename T>
static int BinarySearch(cli::array <T> ^ array, int index, int length, T value);
public static int BinarySearch<T> (T[] array, int index, int length, T value);
static member BinarySearch : 'T[] * int * int * 'T -> int
Public Shared Function BinarySearch(Of T) (array As T(), index As Integer, length As Integer, value As T) As Integer
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的一维、从零开始的 Array 排序。
- index
- Int32
要搜索的范围的起始索引。
- length
- Int32
要搜索的范围的长度。
- value
- T
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
T 不实现 IComparable<T> 泛型接口。
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果数组不包含指定的值,该方法将返回一个负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引等于数组的大小,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
T 必须实现用于比较的 IComparable<T> 泛型接口。
array 的元素必须已根据 IComparable<T> 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable<T> 实现。 也就是说,IComparable<T> 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 nlength。
另请参阅
- IComparable<T>
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作
适用于
BinarySearch<T>(T[], Int32, Int32, T, IComparer<T>)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
使用指定的 IComparer<T> 泛型接口在一维排序数组中搜索值的范围。
public:
generic <typename T>
static int BinarySearch(cli::array <T> ^ array, int index, int length, T value, System::Collections::Generic::IComparer<T> ^ comparer);
public static int BinarySearch<T> (T[] array, int index, int length, T value, System.Collections.Generic.IComparer<T> comparer);
public static int BinarySearch<T> (T[] array, int index, int length, T value, System.Collections.Generic.IComparer<T>? comparer);
static member BinarySearch : 'T[] * int * int * 'T * System.Collections.Generic.IComparer<'T> -> int
Public Shared Function BinarySearch(Of T) (array As T(), index As Integer, length As Integer, value As T, comparer As IComparer(Of T)) As Integer
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的一维、从零开始的 Array 排序。
- index
- Int32
要搜索的范围的起始索引。
- length
- Int32
要搜索的范围的长度。
- value
- T
要搜索的对象。
返回
在指定的 array中指定 value 的索引(如果找到 value);否则为负数。 如果未找到 value,并且 value 小于 array中的一个或多个元素,则返回的负数是大于 value的第一个元素索引的按位补数。 如果未找到 value 并且 value 大于 array中的所有元素,则返回的负数是位补数(最后一个元素的索引加 1)。 如果使用非排序 array调用此方法,则返回值可能不正确,并且可以返回负数,即使 array中存在 value 也是如此。
例外
array
null。
comparer
null,T 不实现 IComparable<T> 泛型接口。
注解
此方法不支持搜索包含负索引的数组。 在调用此方法之前,必须对 array 进行排序。
如果数组不包含指定的值,该方法将返回一个负整数。 可以将按位补数运算符(~ 在 C# 中,在 Visual Basic 中 Not)应用于负结果以生成索引。 如果此索引等于数组的大小,则数组中没有大于 value 的元素。 否则,它是大于 value的第一个元素的索引。
比较器自定义元素的比较方式。 例如,可以使用 System.Collections.CaseInsensitiveComparer 作为比较器来执行不区分大小写的字符串搜索。
如果未 nullcomparer,则使用指定的 IComparer<T> 泛型接口实现将 array 的元素与指定的值进行比较。
array 的元素必须已根据 comparer定义的排序顺序按递增值进行排序;否则,结果可能不正确。
如果 comparernull,则使用为类型 T提供的 IComparable<T> 泛型接口实现完成比较。
array 的元素必须已根据 IComparable<T> 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。
允许重复元素。 如果 Array 包含等于 value的多个元素,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null 始终可与任何其他引用类型进行比较;因此,使用 IComparable<T>时,与 null 的比较不会生成异常。
注意
对于测试的每个元素,即使 valuenull,value 也会传递到适当的 IComparable<T> 实现。 也就是说,IComparable<T> 实现确定给定元素与 null的比较方式。
此方法是 O(log n) 操作,其中 nlength。
另请参阅
- IComparer<T>
- IComparable<T>
- Sort
- 在数组 中执行 Culture-Insensitive 字符串操作