ArrayList.BinarySearch 方法

定义

使用二进制搜索算法在排序 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 (object value);
public virtual int BinarySearch (object? value);

参数

value
Object

要查找的 Object。 该值可以 null

返回

如果找到 value,则排序 ArrayListvalue 的从零开始的索引;否则为负数,即下一个大于 value 的元素的索引的按位补数;如果没有较大的元素,则为 Count的按位补数。

例外

valueArrayList 的元素都不实现 IComparable 接口。

value 的类型与 ArrayList的元素不同。

示例

下面的代码示例演示如何使用 BinarySearchArrayList中查找特定对象。

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.
*/

注解

value 参数和 ArrayList 的每个元素都必须实现用于比较的 IComparable 接口。 ArrayList 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。

null 与任何类型进行比较,在使用 IComparable时不会生成异常。 排序时,null 被视为小于任何其他对象。

如果 ArrayList 包含多个具有相同值的元素,该方法只返回其中一个匹配项,并且可能会返回任何一个匹配项,不一定返回第一个。

如果 ArrayList 不包含指定值,该方法将返回负整数。 可以将按位补运算 (~) 应用于此负整数,以获取大于搜索值的第一个元素的索引。 将值插入 ArrayList时,此索引应用作插入点来维护排序顺序。

此方法是 O(log n) 操作,其中 nCount

另请参阅

  • 在集合 中执行 Culture-Insensitive 字符串操作

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

BinarySearch(Object, IComparer)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

使用指定的比较器搜索整个排序 ArrayList 元素,并返回该元素的从零开始的索引。

public virtual int BinarySearch (object value, System.Collections.IComparer comparer);
public virtual int BinarySearch (object? value, System.Collections.IComparer? comparer);

参数

value
Object

要查找的 Object。 该值可以 null

comparer
IComparer

比较元素时要使用的 IComparer 实现。

-或-

null 使用默认比较器,即每个元素的 IComparable 实现。

返回

如果找到 value,则排序 ArrayListvalue 的从零开始的索引;否则为负数,即下一个大于 value 的元素的索引的按位补数;如果没有较大的元素,则为 Count的按位补数。

例外

comparernull 的,valueArrayList 的元素都不实现 IComparable 接口。

comparernull 的,value 的类型与 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
//

注解

比较器自定义元素的比较方式。 例如,可以使用 CaseInsensitiveComparer 实例作为比较器来执行不区分大小写的字符串搜索。

如果提供了 comparer,则使用指定的 IComparer 实现将 ArrayList 的元素与指定值进行比较。 ArrayList 的元素必须已根据 comparer定义的排序顺序按递增值进行排序;否则,结果可能不正确。

如果 comparernull,则使用元素本身或指定值提供的 IComparable 实现完成比较。 ArrayList 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。

null 与任何类型进行比较,在使用 IComparable时不会生成异常。 排序时,null 被视为小于任何其他对象。

如果 ArrayList 包含多个具有相同值的元素,该方法只返回其中一个匹配项,并且可能会返回任何一个匹配项,不一定返回第一个。

如果 ArrayList 不包含指定值,该方法将返回负整数。 可以将按位补运算 (~) 应用于此负整数,以获取大于搜索值的第一个元素的索引。 将值插入 ArrayList时,此索引应用作插入点来维护排序顺序。

此方法是 O(log n) 操作,其中 nCount

另请参阅

  • 在集合 中执行 Culture-Insensitive 字符串操作

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

BinarySearch(Int32, Int32, Object, IComparer)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

使用指定的比较器在排序 ArrayList 中搜索元素的范围,并返回元素的从零开始的索引。

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);

参数

index
Int32

要搜索的范围从零开始的索引。

count
Int32

要搜索的范围的长度。

value
Object

要查找的 Object。 该值可以 null

comparer
IComparer

比较元素时要使用的 IComparer 实现。

-或-

null 使用默认比较器,即每个元素的 IComparable 实现。

返回

如果找到 value,则排序 ArrayListvalue 的从零开始的索引;否则为负数,即下一个大于 value 的元素的索引的按位补数;如果没有较大的元素,则为 Count的按位补数。

例外

indexcount 不表示 ArrayList中的有效范围。

-或-

comparernull 的,valueArrayList 的元素都不实现 IComparable 接口。

comparernull 的,value 的类型与 ArrayList的元素不同。

index 小于零。

-或-

count 小于零。

注解

比较器自定义元素的比较方式。 例如,可以使用 CaseInsensitiveComparer 实例作为比较器来执行不区分大小写的字符串搜索。

如果提供了 comparer,则使用指定的 IComparer 实现将 ArrayList 的元素与指定值进行比较。 ArrayList 的元素必须已根据 comparer定义的排序顺序按递增值进行排序;否则,结果可能不正确。

如果 comparernull,则使用元素本身或指定值提供的 IComparable 实现完成比较。 ArrayList 的元素必须已根据 IComparable 实现定义的排序顺序按递增值进行排序;否则,结果可能不正确。

null 与任何类型进行比较,在使用 IComparable时不会生成异常。 排序时,null 被视为小于任何其他对象。

如果 ArrayList 包含多个具有相同值的元素,该方法只返回其中一个匹配项,并且可能会返回任何一个匹配项,不一定返回第一个。

如果 ArrayList 不包含指定值,该方法将返回负整数。 可以将按位补运算 (~) 应用于此负整数,以获取大于搜索值的第一个元素的索引。 将值插入 ArrayList时,此索引应用作插入点来维护排序顺序。

此方法是 O(log n) 操作,其中 ncount

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0