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)
使用由数组中每个元素和指定对象实现的 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
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
必须实现IComparable接口的每个元素array
,该接口用于比较。 必须已根据实现定义的IComparable排序顺序对元素array
进行递增值排序;否则,结果可能不正确。
备注
如果未value
实现IComparable接口,则搜索开始前不会测试IComparable其元素array
。 如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 Array如果包含等于的多个元素value
,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null
始终可与任何其他引用类型进行比较;因此,与 null
不生成异常的比较。
备注
对于测试的每个元素, value
都会传递到适当的 IComparable 实现,即使 value
是 null
。 也就是说, IComparable 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志 n
) 操作,其位置 n
为 Length array
.
另请参阅
适用于
BinarySearch(Array, Object, IComparer)
使用指定 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
array
为 null
。
array
是多维的。
comparer
为 null
,并且 value
的类型与 array
的元素不兼容。
comparer
为 null
,value
不实现 IComparable 接口,并且搜索遇到不实现 IComparable 接口的元素。
注解
此方法不支持搜索包含负索引的数组。 array
必须在调用此方法之前进行排序。
Array如果不包含指定值,该方法将返回负整数。 可以在 C Visual Basic) # Not
中将按位补数运算符 (~ 应用于负结果,以生成索引。 如果此索引大于数组的上限,则数组中没有大于 value
元素。 否则,它是大于 value
第一个元素的索引。
比较器自定义元素的比较方式。 例如,可以使用 System.Collections.CaseInsensitiveComparer 比较器来执行不区分大小写的字符串搜索。
null
如果没有comparer
,则array
使用指定的实现将元素与指定IComparer值进行比较。 array
必须已根据定义的comparer
排序顺序对元素进行递增值排序;否则,结果可能不正确。
null
如果是comparer
,则使用IComparable元素本身或指定值提供的实现完成比较。 必须已根据实现定义的IComparable排序顺序对元素array
进行递增值排序;否则,结果可能不正确。
备注
如果是comparer
且value
未实现IComparable接口,则搜索开始前不会测试IComparable其元素array
。null
如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 Array如果包含等于的多个元素value
,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null
始终可与任何其他引用类型进行比较;因此,与 null
不生成异常的比较。
备注
对于测试的每个元素, value
都会传递到适当的 IComparable 实现,即使 value
是 null
。 也就是说, IComparable 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志 n
) 操作,其位置 n
为 Length array
.
另请参阅
适用于
BinarySearch(Array, Int32, Int32, Object)
使用由一维排序数组中每个元素和指定的值实现的 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
array
为 null
。
array
是多维的。
value
不实现 IComparable 接口,并且搜索遇到不实现 IComparable 接口的元素。
注解
此方法不支持搜索包含负索引的数组。 array
必须在调用此方法之前进行排序。
Array如果不包含指定值,该方法将返回负整数。 可以在 C Visual Basic) # Not
中将按位补数运算符 (~ 应用于负结果,以生成索引。 如果此索引大于数组的上限,则数组中没有大于 value
元素。 否则,它是大于 value
第一个元素的索引。
value
必须实现IComparable接口的每个元素array
,该接口用于比较。 必须已根据实现定义的IComparable排序顺序对元素array
进行递增值排序;否则,结果可能不正确。
备注
如果未value
实现IComparable接口,则搜索开始前不会测试IComparable其元素array
。 如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 Array如果包含等于的多个元素value
,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null
始终可与任何其他引用类型进行比较;因此,与 null
不生成异常的比较。
备注
对于测试的每个元素, value
都会传递到适当的 IComparable 实现,即使 value
是 null
。 也就是说, IComparable 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志n
) 操作,其中n
。length
另请参阅
适用于
BinarySearch(Array, Int32, Int32, Object, IComparer)
使用指定 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
array
为 null
。
array
是多维的。
comparer
为 null
,value
不实现 IComparable 接口,并且搜索遇到不实现 IComparable 接口的元素。
注解
此方法不支持搜索包含负索引的数组。 array
必须在调用此方法之前进行排序。
Array如果不包含指定值,该方法将返回负整数。 可以在 C Visual Basic) # Not
中将按位补数运算符 (~ 应用于负结果,以生成索引。 如果此索引大于数组的上限,则数组中没有大于 value
元素。 否则,它是大于 value
第一个元素的索引。
比较器自定义元素的比较方式。 例如,可以使用 System.Collections.CaseInsensitiveComparer 比较器来执行不区分大小写的字符串搜索。
null
如果没有comparer
,则array
使用指定的实现将元素与指定IComparer值进行比较。 array
必须已根据定义的comparer
排序顺序对元素进行递增值排序;否则,结果可能不正确。
null
如果是comparer
,则使用IComparable元素本身或指定值提供的实现完成比较。 必须已根据实现定义的IComparable排序顺序对元素array
进行递增值排序;否则,结果可能不正确。
备注
如果是comparer
且value
未实现IComparable接口,则搜索开始前不会测试IComparable其元素array
。null
如果搜索遇到未实现 IComparable的元素,则会引发异常。
允许重复元素。 Array如果包含等于的多个元素value
,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null
始终可与任何其他引用类型进行比较;因此,与 null
使用 IComparable时不生成异常的比较。
备注
对于测试的每个元素, value
都会传递到适当的 IComparable 实现,即使 value
是 null
。 也就是说, IComparable 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志n
) 操作,其中n
。length
另请参阅
适用于
BinarySearch<T>(T[], T)
使用由 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
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
泛型方法, showWhere
(F# 示例中的函数) ,如果找到字符串,则显示索引值,否则搜索字符串在数组中时将介于其之间的元素。 如果字符串不在数组中,则索引为负数,因此该方法ShowWhere
采用按位补充 (C# 和 Visual C++ 中的 ~ 运算符,即 F# 中的 ~ 运算符,Xor
在 Visual Basic) -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
比较不会生成异常。
备注
对于测试的每个元素, value
将传递到适当的 IComparable<T> 实现,即使 value
为 null
。 也就是说, IComparable<T> 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志 n
) 操作,其位置 n
为 Length array
.
另请参阅
适用于
BinarySearch<T>(T[], T, IComparer<T>)
使用指定 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
array
为 null
。
comparer
为 null
,并且 value
的类型与 array
的元素不兼容。
comparer
为 null
,并且 T
不实现 IComparable<T> 泛型接口
示例
以下示例演示 Sort<T>(T[], IComparer<T>) 泛型方法重载和 BinarySearch<T>(T[], T, IComparer<T>) 泛型方法重载。
该代码示例定义一个替代比较器,ReverseCompare
用于在 Visual C++) 泛型接口中实现IComparer<string>
Visual BasicIComparer<String^>
中的 (IComparer(Of 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
泛型方法, showWhere
(F# 示例中的函数) ,如果找到字符串,则显示索引值,否则搜索字符串在数组中时将介于其之间的元素。 如果字符串不是数组的 n,则索引为负,因此该方法ShowWhere
采用按位补数 (C# 和 Visual C++ 中的 ~ 运算符, Xor
Visual Basic) 中的 ~ 运算符 -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 器来执行不区分大小写的字符串搜索。
如果 comparer
不是 null
,则使用指定的泛型接口实现将指定值的元素 array
与指定的 IComparer<T> 值进行比较。 必须已根据定义的comparer
排序顺序对值中的元素array
进行排序;否则,结果可能不正确。
null
如果是comparer
,则使用提供的T
泛型接口实现完成IComparable<T>比较。 array
必须根据实现定义的IComparable<T>排序顺序对元素进行递增值排序;否则,结果可能不正确。
备注
如果comparer
不是null
且value
不实现IComparable<T>泛型接口,则搜索开始前不会测试IComparable<T>其array
元素。 如果搜索遇到不实现 IComparable<T>的元素,则会引发异常。
允许重复元素。 Array如果包含等于的多个元素value
,则该方法仅返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null
始终可与任何其他引用类型进行比较;因此,与 null
比较不会生成异常。
备注
对于测试的每个元素, value
将传递到适当的 IComparable<T> 实现,即使 value
为 null
。 也就是说, IComparable<T> 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志 n
) 操作,其位置 n
为 Length array
.
另请参阅
适用于
BinarySearch<T>(T[], Int32, Int32, T)
使用由 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
array
为 null
。
T
不实现 IComparable<T> 泛型接口。
注解
此方法不支持搜索包含负索引的数组。 array
必须在调用此方法之前进行排序。
如果数组不包含指定的值,该方法将返回负整数。 可以在 C Visual Basic) # Not
中将按位补数运算符 (~ 应用于负结果,以生成索引。 如果此索引等于数组的大小,则数组中没有大于 value
元素。 否则,它是大于 value
的第一个元素的索引。
T
必须实现 IComparable<T> 用于比较的泛型接口。 array
必须根据实现定义的IComparable<T>排序顺序对元素进行递增值排序;否则,结果可能不正确。
允许重复元素。 Array如果包含等于的多个元素value
,则该方法仅返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null
始终可与任何其他引用类型进行比较;因此,与 null
比较不会生成异常。
备注
对于测试的每个元素, value
将传递到适当的 IComparable<T> 实现,即使 value
为 null
。 也就是说, IComparable<T> 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志n
) 操作,其中n
。length
另请参阅
适用于
BinarySearch<T>(T[], Int32, Int32, T, IComparer<T>)
使用指定 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
要搜索的对象。
返回
如果找到 value
,则为指定 array
中的指定 value
的索引;否则为负数。 如果找不到 value
且 value
小于 array
中的一个或多个元素,则返回的负数是大于 value
的第一个元素的索引的按位求补。 如果找不到 value
且 value
大于 array
中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。 如果使用非排序的 array
调用此方法,返回值则可能不正确并且可能会返回负数,即使 value
存在于 array
中也是如此。
例外
array
为 null
。
comparer
为 null
,并且 T
不实现 IComparable<T> 泛型接口。
注解
此方法不支持搜索包含负索引的数组。 array
必须在调用此方法之前进行排序。
如果数组不包含指定的值,该方法将返回负整数。 可以在 C Visual Basic) # Not
中将按位补数运算符 (~ 应用于负结果,以生成索引。 如果此索引等于数组的大小,则数组中没有大于 value
元素。 否则,它是大于 value
第一个元素的索引。
比较器自定义元素的比较方式。 例如,可以使用 System.Collections.CaseInsensitiveComparer 比较器来执行不区分大小写的字符串搜索。
null
如果没有comparer
,则使用指定的泛型接口实现将指定值的元素array
与指定IComparer<T>值进行比较。 array
必须已根据定义的comparer
排序顺序对元素进行递增值排序;否则,结果可能不正确。
null
如果是comparer
,则使用IComparable<T>为类型T
提供的泛型接口实现完成比较。 必须已根据实现定义的IComparable<T>排序顺序对元素array
进行递增值排序;否则,结果可能不正确。
允许重复元素。 Array如果包含等于的多个元素value
,该方法只返回其中一个匹配项的索引,不一定返回第一个元素的索引。
null
始终可与任何其他引用类型进行比较;因此,与 null
使用 IComparable<T>时不生成异常的比较。
备注
对于测试的每个元素, value
都会传递到适当的 IComparable<T> 实现,即使 value
是 null
。 也就是说, IComparable<T> 实现确定给定元素与该元素的比较 null
方式。
此方法是 O (日志n
) 操作,其中n
。length