ArrayList.BinarySearch 方法

定義

使用二進位搜尋演算法,在排序 ArrayList 或其中一部分中尋找特定元素。

多載

BinarySearch(Object)

使用預設比較子搜尋項目的整個排序 ArrayList,並傳回專案以零起始的索引。

BinarySearch(Object, IComparer)

使用指定的比較子搜尋整個已排序的 ArrayList 專案,並傳回專案以零起始的索引。

BinarySearch(Int32, Int32, Object, IComparer)

使用指定的比較子搜尋已排序之 ArrayList 中的專案範圍,並傳回專案以零起始的索引。

BinarySearch(Object)

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
ArrayList.cs

使用預設比較子搜尋項目的整個排序 ArrayList,並傳回專案以零起始的索引。

public virtual int BinarySearch (object value);
public virtual int BinarySearch (object? value);

參數

value
Object

要尋找的 Object。 值可以是 null

傳回

如果找到 value,則為排序 ArrayList中以零起始的 value 索引;否則為負數,這是大於 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 實作所定義的排序順序,以遞增值排序:否則,結果可能不正確。

使用 IComparable時,允許 null 與任何類型進行比較,而且不會產生例外狀況。 排序時,會將 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

BinarySearch(Object, IComparer)

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
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,則為排序 ArrayList中以零起始的 value 索引;否則為負數,這是大於 value 的下一個專案索引的位補碼,如果沒有較大的元素,則為 Count的位補碼。

例外狀況

comparernull,而且 valueArrayList 的元素都不會實作 IComparable 介面。

comparernull,且 valueArrayList元素的類型不同。

範例

下列範例會建立彩色動物的 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 實作所定義的排序順序,以遞增值排序:否則,結果可能不正確。

使用 IComparable時,允許 null 與任何類型進行比較,而且不會產生例外狀況。 排序時,會將 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

BinarySearch(Int32, Int32, Object, IComparer)

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
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,則為排序 ArrayList中以零起始的 value 索引;否則為負數,這是大於 value 的下一個專案索引的位補碼,如果沒有較大的元素,則為 Count的位補碼。

例外狀況

indexcount 不表示 ArrayList中的有效範圍。

-或-

comparernull,而且 valueArrayList 的元素都不會實作 IComparable 介面。

comparernull,且 valueArrayList元素的類型不同。

index 小於零。

-或-

count 小於零。

備註

比較子會自定義項目的比較方式。 例如,您可以使用 CaseInsensitiveComparer 實例作為比較子來執行不區分大小寫的字串搜尋。

如果提供 comparer,則會使用指定的 IComparer 實作,將 ArrayList 的元素與指定的值進行比較。 ArrayList 的元素必須根據 comparer所定義的排序順序,以遞增值來排序;否則,結果可能不正確。

如果 comparernull,則比較會使用專案本身或指定值所提供的 IComparable 實作來完成。 ArrayList 的元素必須已根據 IComparable 實作所定義的排序順序,以遞增值排序:否則,結果可能不正確。

使用 IComparable時,允許 null 與任何類型進行比較,而且不會產生例外狀況。 排序時,會將 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