既定の比較演算子を使用して、並べ替えられた要素の ArrayList 全体を検索し、その要素の 0 から始まるインデックスを返します。
Overloads Public Overridable Function BinarySearch( _
ByVal value As Object _) As Integer
[C#]
public virtual int BinarySearch(objectvalue);
[C++]
public: virtual int BinarySearch(Object* value);
[JScript]
public function BinarySearch(
value : Object) : int;
パラメータ
- value
検索する Object 。値は null 参照 (Visual Basic では Nothing) に設定できます。
戻り値
value が見つかった場合は、並べ替えられた ArrayList 内の value の 0 から始まるインデックス。見つからなかった場合は、負の値。これは、 value の次に大きい要素のインデックスのビットごとの補数です。ただし、大きい要素が存在しない場合は、 Count のビットごとの補数です。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | value と、 ArrayList の要素が、いずれも IComparable インターフェイスを実装していません。 |
InvalidOperationException | value が、 ArrayList の要素と同じ型ではありません。 |
解説
value パラメータと、 ArrayList の各要素は、 IComparable インターフェイスを実装する必要があります。このインターフェイスは、比較に使用されます。 ArrayList が IComparable の実装に従って並べ替えられていない場合は、結果が正しくない可能性があります。
null 参照 (Visual Basic では Nothing) を任意の型と比較できます。このような比較を行っても、 IComparable を使用した場合に例外が生成されることはありません。並べ替え処理では、 null 参照 (Nothing) は、他のすべてのオブジェクトより小さいと見なされます。
ArrayList に同じ値の複数の要素が格納されている場合、メソッドは 1 つの要素だけを返します。必ずしも最初の要素ではなく、任意の要素が返されます。
指定した値が ArrayList に格納されていない場合、このメソッドは負の整数を返します。この負の整数に、ビットごとの補数演算 (~) を適用して、検索値よりも大きい最初の要素のインデックスを取得できます。値を ArrayList に挿入する場合、このインデックスを挿入ポイントとして使用して、並べ替え順を維持する必要があります。
このメソッドは O(log2 n) 操作です。 n は、所定の範囲内の要素数を示します。
使用例
BinarySearch を使用して、 ArrayList の特定のオブジェクトを検索する方法の例を次に示します。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new 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 myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
End While
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.
[C#]
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
for ( int i = 0; i <= 4; i++ )
myAL.Add( i*2 );
// Displays the ArrayList.
Console.WriteLine( "The Int32 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 ) {
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
/*
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.
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
void FindMyObject( ArrayList* myList, Object* myObject );
void PrintValues( IEnumerable* myList );
void main() {
// Creates and initializes a new ArrayList instance.
ArrayList* myAL = new ArrayList();
for ( int i = 0; i <= 4; i++ )
myAL->Add( __box(i*2) );
// Displays the ArrayList.
Console::WriteLine( "The Int32 ArrayList contains the following:" );
PrintValues( myAL );
// Locates a specific object that does not exist in the ArrayList.
Object* myObjectOdd = __box(3);
FindMyObject( myAL, myObjectOdd );
// Locates an object that exists in the ArrayList.
Object* myObjectEven = __box(6);
FindMyObject( myAL, myObjectEven );
}
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, __box(~myIndex) );
else
Console::WriteLine( "The object to search for ({0}) is at index {1}.", myObject, __box(myIndex) );
}
void PrintValues( IEnumerable* myList ) {
System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
while ( myEnumerator->MoveNext() )
Console::Write( "\t{0}", myEnumerator->Current );
Console::WriteLine();
}
/*
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.
*/
[JScript]
import System;
import System.Collections;
// Creates and initializes a new ArrayList.
var myAL : ArrayList = new ArrayList();
for ( var i : int = 0; i <= 4; i++ )
myAL.Add( int(i*2 ));
// Displays the ArrayList.
Console.WriteLine( "The Int32 ArrayList contains the following:" );
PrintValues( myAL );
// Locates a specific object that does not exist in the ArrayList.
var myObjectOdd = 3;
FindMyObject( myAL, myObjectOdd );
// Locates an object that exists in the ArrayList.
var myObjectEven = 6;
FindMyObject( myAL, myObjectEven );
function FindMyObject( myList : ArrayList, myObject ) {
var myIndex : int = 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 );
}
function PrintValues( myList : IEnumerable) {
var myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
/*
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.
*/
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard
参照
ArrayList クラス | ArrayList メンバ | System.Collections 名前空間 | ArrayList.BinarySearch オーバーロードの一覧 | カルチャを認識しないコレクションの操作の実行