Array.BinarySearch Methode
Definition
Überlädt
BinarySearch(Array, Object) |
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der IComparable-Schnittstelle, die von jedem Element des Arrays und durch das angegebene Objekt implementiert wird, nach einem bestimmten Element.Searches an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object. |
BinarySearch(Array, Object, IComparer) |
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der angegebenen IComparer-Schnittstelle nach einem Wert.Searches an entire one-dimensional sorted array for a value using the specified IComparer interface. |
BinarySearch(Array, Int32, Int32, Object) |
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der IComparable-Schnittstelle, die von jedem Element des Arrays und durch den angegebenen Wert implementiert wird, nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value. |
BinarySearch(Array, Int32, Int32, Object, IComparer) |
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der angegebenen IComparer-Schnittstelle nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer interface. |
BinarySearch<T>(T[], T) |
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der generischen IComparable<T>-Schnittstelle, die von jedem Element des Array und vom angegebenen Objekt implementiert wird, nach einem bestimmten Element.Searches an entire one-dimensional sorted array for a specific element, using the IComparable<T> generic interface implemented by each element of the Array and by the specified object. |
BinarySearch<T>(T[], T, IComparer<T>) |
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der angegebenen generischen IComparer<T>-Schnittstelle nach einem Wert.Searches an entire one-dimensional sorted array for a value using the specified IComparer<T> generic interface. |
BinarySearch<T>(T[], Int32, Int32, T) |
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der generischen IComparable<T>-Schnittstelle, die von jedem Element des Array und vom angegebenen Wert implementiert wird, nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the IComparable<T> generic interface implemented by each element of the Array and by the specified value. |
BinarySearch<T>(T[], Int32, Int32, T, IComparer<T>) |
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der angegebenen generischen IComparer<T>-Schnittstelle nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer<T> generic interface. |
BinarySearch(Array, Object)
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der IComparable-Schnittstelle, die von jedem Element des Arrays und durch das angegebene Objekt implementiert wird, nach einem bestimmten Element.Searches an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object.
public:
static int BinarySearch(Array ^ array, System::Object ^ value);
public static int BinarySearch (Array array, object value);
static member BinarySearch : Array * obj -> int
Parameter
- array
- Array
Das zu durchsuchende sortierte eindimensionale Array.The sorted one-dimensional Array to search.
- value
- Object
Das Objekt, nach dem gesucht werden soll.The object to search for.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
array
ist mehrdimensional.array
is multidimensional.
value
verfügt über einen Typ, der nicht mit den Elementen von array
kompatibel ist.value
is of a type that is not compatible with the elements of array
.
value
implementiert nicht die IComparable-Schnittstelle, und bei der Suche wird ein Element gefunden, das die IComparable-Schnittstelle nicht implementiert.value
does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie BinarySearch verwendet wird, um ein bestimmtes Objekt in einem Arrayzu suchen.The following code example shows how to use BinarySearch to locate a specific object in an Array.
Hinweis
Das Array wird mit seinen Elementen in aufsteigender Sortierreihenfolge erstellt.The array is created with its elements in ascending sort order. Die BinarySearch-Methode erfordert, dass das Array in aufsteigender Reihenfolge sortiert wird.The BinarySearch method requires the array to be sorted in ascending order.
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.
using System;
public class SamplesArray
{
public static void Main()
{
// Creates and initializes a new Array.
Array myIntArray = Array.CreateInstance(typeof(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.
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 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.
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.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn das Array den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If the Array does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index ein Wert größer als die obere Grenze des Arrays ist, sind keine Elemente größer als value
im Array.If this index is one greater than the upper bound of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
Entweder value
oder jedes Element von array
muss die IComparable-Schnittstelle implementieren, die für Vergleiche verwendet wird.Either value
or every element of array
must implement the IComparable interface, which is used for comparisons. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
Hinweis
Wennvalue
die IComparable-Schnittstelle nicht implementiert, werden die Elemente von array
nicht auf IComparable getestet, bevor die Suche beginnt.Ifvalue
does not implement the IComparable interface, the elements of array
are not tested for IComparable before the search begins. Eine-Ausnahme wird ausgelöst, wenn bei der Suche ein Element gefunden wird, das IComparablenicht implementiert.An exception is thrown if the search encounters an element that does not implement IComparable.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. aus diesem Grund generieren Vergleiche mit null
keine Ausnahme.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable implementation, even if value
is null
. Das heißt, die IComparable-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
der Length von array
ist.This method is an O(log n
) operation, where n
is the Length of array
.
Siehe auch
BinarySearch(Array, Object, IComparer)
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der angegebenen IComparer-Schnittstelle nach einem Wert.Searches an entire one-dimensional sorted array for a value using the specified IComparer interface.
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);
static member BinarySearch : Array * obj * System.Collections.IComparer -> int
Parameter
- array
- Array
Das zu durchsuchende sortierte eindimensionale Array.The sorted one-dimensional Array to search.
- value
- Object
Das Objekt, nach dem gesucht werden soll.The object to search for.
- comparer
- IComparer
Die IComparer-Implementierung, die beim Vergleich von Elementen verwendet werden soll.The IComparer implementation to use when comparing elements.
- oder --or-
null
, wenn die IComparable-Implementierung des jeweiligen Elements verwendet werden soll.null
to use the IComparable implementation of each element.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
array
ist mehrdimensional.array
is multidimensional.
comparer
ist null
und value
verfügt über einen Typ, der nicht mit den Elementen von array
kompatibel ist.comparer
is null
, and value
is of a type that is not compatible with the elements of array
.
comparer
istnull
, value
implementiert nicht die IComparable-Schnittstelle, und bei der Suche wird ein Element gefunden, das die IComparable-Schnittstelle nicht implementiert.comparer
is null
, value
does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn das Array den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If the Array does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index ein Wert größer als die obere Grenze des Arrays ist, sind keine Elemente größer als value
im Array.If this index is one greater than the upper bound of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
Der Vergleich passt an, wie die Elemente verglichen werden.The comparer customizes how the elements are compared. Beispielsweise können Sie eine System.Collections.CaseInsensitiveComparer als Vergleichs Operator verwenden, um Zeichen folgen suchen ohne Berücksichtigung der Groß-/Kleinschreibung auszuführen.For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches.
Wenn comparer
nicht null
ist, werden die Elemente array
unter Verwendung der angegebenen IComparer Implementierung mit dem angegebenen Wert verglichen.If comparer
is not null
, the elements of array
are compared to the specified value using the specified IComparer implementation. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der durch comparer
definierten Sortierreihenfolge sortiert werden. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by comparer
; otherwise, the result might be incorrect.
Wenncomparer
null
ist, erfolgt der Vergleich mithilfe der IComparable-Implementierung, die vom Element selbst oder durch den angegebenen Wert bereitgestellt wird.Ifcomparer
is null
, the comparison is done using the IComparable implementation provided by the element itself or by the specified value. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
Hinweis
Wenn comparer
null
und value
die IComparable Schnittstelle nicht implementiert, werden die Elemente von array
vor Beginn der Suche nicht auf IComparable getestet.If comparer
is null
and value
does not implement the IComparable interface, the elements of array
are not tested for IComparable before the search begins. Eine-Ausnahme wird ausgelöst, wenn bei der Suche ein Element gefunden wird, das IComparablenicht implementiert.An exception is thrown if the search encounters an element that does not implement IComparable.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. aus diesem Grund generieren Vergleiche mit null
keine Ausnahme.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable implementation, even if value
is null
. Das heißt, die IComparable-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
der Length von array
ist.This method is an O(log n
) operation, where n
is the Length of array
.
Siehe auch
BinarySearch(Array, Int32, Int32, Object)
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der IComparable-Schnittstelle, die von jedem Element des Arrays und durch den angegebenen Wert implementiert wird, nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value.
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);
static member BinarySearch : Array * int * int * obj -> int
Parameter
- array
- Array
Das zu durchsuchende sortierte eindimensionale Array.The sorted one-dimensional Array to search.
- index
- Int32
Der Startindex des zu durchsuchenden Bereichs.The starting index of the range to search.
- length
- Int32
Die Länge des zu durchsuchenden Bereichs.The length of the range to search.
- value
- Object
Das Objekt, nach dem gesucht werden soll.The object to search for.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
array
ist mehrdimensional.array
is multidimensional.
index
ist kleiner als die untere array
-Grenze.index
is less than the lower bound of array
.
- oder --or-
length
ist kleiner als Null.length
is less than zero.
index
und length
geben keinen gültigen Bereich im array
an.index
and length
do not specify a valid range in array
.
- oder --or-
value
verfügt über einen Typ, der nicht mit den Elementen von array
kompatibel ist.value
is of a type that is not compatible with the elements of array
.
value
implementiert nicht die IComparable-Schnittstelle, und bei der Suche wird ein Element gefunden, das die IComparable-Schnittstelle nicht implementiert.value
does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn das Array den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If the Array does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index ein Wert größer als die obere Grenze des Arrays ist, sind keine Elemente größer als value
im Array.If this index is one greater than the upper bound of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
Entweder value
oder jedes Element von array
muss die IComparable-Schnittstelle implementieren, die für Vergleiche verwendet wird.Either value
or every element of array
must implement the IComparable interface, which is used for comparisons. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
Hinweis
Wenn value
die IComparable-Schnittstelle nicht implementiert, werden die Elemente von array
nicht auf IComparable getestet, bevor die Suche beginnt.If value
does not implement the IComparable interface, the elements of array
are not tested for IComparable before the search begins. Eine-Ausnahme wird ausgelöst, wenn bei der Suche ein Element gefunden wird, das IComparablenicht implementiert.An exception is thrown if the search encounters an element that does not implement IComparable.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. aus diesem Grund generieren Vergleiche mit null
keine Ausnahme.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable implementation, even if value
is null
. Das heißt, die IComparable-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
length
ist.This method is an O(log n
) operation, where n
is length
.
Siehe auch
BinarySearch(Array, Int32, Int32, Object, IComparer)
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der angegebenen IComparer-Schnittstelle nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer interface.
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);
static member BinarySearch : Array * int * int * obj * System.Collections.IComparer -> int
Parameter
- array
- Array
Das zu durchsuchende sortierte eindimensionale Array.The sorted one-dimensional Array to search.
- index
- Int32
Der Startindex des zu durchsuchenden Bereichs.The starting index of the range to search.
- length
- Int32
Die Länge des zu durchsuchenden Bereichs.The length of the range to search.
- value
- Object
Das Objekt, nach dem gesucht werden soll.The object to search for.
- comparer
- IComparer
Die IComparer-Implementierung, die beim Vergleich von Elementen verwendet werden soll.The IComparer implementation to use when comparing elements.
- oder --or-
null
, wenn die IComparable-Implementierung des jeweiligen Elements verwendet werden soll.null
to use the IComparable implementation of each element.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
array
ist mehrdimensional.array
is multidimensional.
index
ist kleiner als die untere array
-Grenze.index
is less than the lower bound of array
.
- oder --or-
length
ist kleiner als Null.length
is less than zero.
index
und length
geben keinen gültigen Bereich im array
an.index
and length
do not specify a valid range in array
.
- oder --or-
comparer
ist null
und value
verfügt über einen Typ, der nicht mit den Elementen von array
kompatibel ist.comparer
is null
, and value
is of a type that is not compatible with the elements of array
.
comparer
istnull
, value
implementiert nicht die IComparable-Schnittstelle, und bei der Suche wird ein Element gefunden, das die IComparable-Schnittstelle nicht implementiert.comparer
is null
, value
does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn das Array den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If the Array does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index ein Wert größer als die obere Grenze des Arrays ist, sind keine Elemente größer als value
im Array.If this index is one greater than the upper bound of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
Der Vergleich passt an, wie die Elemente verglichen werden.The comparer customizes how the elements are compared. Beispielsweise können Sie eine System.Collections.CaseInsensitiveComparer als Vergleichs Operator verwenden, um Zeichen folgen suchen ohne Berücksichtigung der Groß-/Kleinschreibung auszuführen.For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches.
Wenn comparer
nicht null
ist, werden die Elemente array
unter Verwendung der angegebenen IComparer Implementierung mit dem angegebenen Wert verglichen.If comparer
is not null
, the elements of array
are compared to the specified value using the specified IComparer implementation. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der durch comparer
definierten Sortierreihenfolge sortiert werden. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by comparer
; otherwise, the result might be incorrect.
Wenn comparer
null
ist, erfolgt der Vergleich mithilfe der IComparable-Implementierung, die vom Element selbst oder durch den angegebenen Wert bereitgestellt wird.If comparer
is null
, the comparison is done using the IComparable implementation provided by the element itself or by the specified value. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
Hinweis
Wenn comparer
null
und value
die IComparable Schnittstelle nicht implementiert, werden die Elemente von array
vor Beginn der Suche nicht auf IComparable getestet.If comparer
is null
and value
does not implement the IComparable interface, the elements of array
are not tested for IComparable before the search begins. Eine-Ausnahme wird ausgelöst, wenn bei der Suche ein Element gefunden wird, das IComparablenicht implementiert.An exception is thrown if the search encounters an element that does not implement IComparable.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. Daher generieren Vergleiche mit null
keine Ausnahme bei der Verwendung IComparable.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception when using IComparable.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable implementation, even if value
is null
. Das heißt, die IComparable-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
length
ist.This method is an O(log n
) operation, where n
is length
.
Siehe auch
BinarySearch<T>(T[], T)
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der generischen IComparable<T>-Schnittstelle, die von jedem Element des Array und vom angegebenen Objekt implementiert wird, nach einem bestimmten Element.Searches an entire one-dimensional sorted array for a specific element, using the IComparable<T> generic interface implemented by each element of the Array and by the specified object.
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
Typparameter
- T
Der Typ der Elemente des Arrays.The type of the elements of the array.
Parameter
- array
- T[]
Das zu durchsuchende sortierte eindimensionale und nullbasierte Array.The sorted one-dimensional, zero-based Array to search.
- value
- T
Das Objekt, nach dem gesucht werden soll.The object to search for.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
Die generische IComparable<T>-Schnittstelle wird von T
nicht implementiert.T
does not implement the IComparable<T> generic interface.
Beispiele
Im folgenden Codebeispiel wird die Sort<T>(T[]) generische Methoden Überladung und die BinarySearch<T>(T[], T) generische Methoden Überladung veranschaulicht.The following code example demonstrates the Sort<T>(T[]) generic method overload and the BinarySearch<T>(T[], T) generic method overload. Ein Array von Zeichen folgen wird erstellt, in keiner bestimmten Reihenfolge.An array of strings is created, in no particular order.
Das Array wird angezeigt, sortiert und erneut angezeigt.The array is displayed, sorted, and displayed again. Arrays müssen sortiert werden, um die BinarySearch-Methode zu verwenden.Arrays must be sorted in order to use the BinarySearch method.
Hinweis
Die Aufrufe der generischen Methoden Sort und BinarySearch unterscheiden sich nicht von den Aufrufen ihrer nicht generischen Entsprechungen, da Visual Basic C#,, C++ und den Typ des generischen Typparameters vom Typ des ersten Arguments ableiten.The calls to the Sort and BinarySearch generic methods do not look any different from calls to their nongeneric counterparts, because Visual Basic, C#, and C++ infer the type of the generic type parameter from the type of the first argument. Wenn Sie den Ildasm. exe (IL-Disassembler) verwenden, um die MSIL (Microsoft Intermediate Language) zu überprüfen, können Sie sehen, dass die generischen Methoden aufgerufen werden.If you use the Ildasm.exe (IL Disassembler) to examine the Microsoft intermediate language (MSIL), you can see that the generic methods are being called.
Die generische Methoden Überladung von BinarySearch<T>(T[], T) wird dann verwendet, um nach zwei Zeichen folgen zu suchen, eine, die nicht im-Array ist, und eine, die ist.The BinarySearch<T>(T[], T) generic method overload is then used to search for two strings, one that is not in the array and one that is. Das Array und der Rückgabewert der BinarySearch-Methode werden an die generische Methode ShowWhere
übergeben, die den Indexwert anzeigt, wenn die Zeichenfolge gefunden wird, und andernfalls die Elemente, zwischen denen die Such Zeichenfolge fällt, wenn Sie sich im Array befindet.The array and the return value of the BinarySearch method are passed to the ShowWhere
generic method, which displays the index value if the string is found, and otherwise the elements the search string would fall between if it were in the array. Der Index ist negativ, wenn die Zeichenfolge nicht im Array enthalten ist, sodass die ShowWhere
-Methode das bitweise Komplement (der ~ C# -Operator C++in und Visual, Xor
-1 in Visual Basic) übernimmt, um den Index des ersten Elements in der Liste zu erhalten, das größer als die Such Zeichenfolge ist.The index is negative if the string is not in the array, so the ShowWhere
method takes the bitwise complement (the ~ operator in C# and Visual C++, Xor
-1 in Visual Basic) to obtain the index of the first element in the list that is larger than the search string.
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.
*/
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.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn array
den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If array
does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index gleich der Größe des Arrays ist, sind keine Elemente größer als value
im Array.If this index is equal to the size of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
T
müssen die IComparable<T> generische-Schnittstelle implementieren, die für Vergleiche verwendet wird.T
must implement the IComparable<T> generic interface, which is used for comparisons. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable<T> Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable<T> implementation; otherwise, the result might be incorrect.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. aus diesem Grund generieren Vergleiche mit null
keine Ausnahme.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable<T> Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable<T> implementation, even if value
is null
. Das heißt, die IComparable<T>-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable<T> implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
der Length von array
ist.This method is an O(log n
) operation, where n
is the Length of array
.
Siehe auch
BinarySearch<T>(T[], T, IComparer<T>)
Durchsucht ein ganzes sortiertes eindimensionales Array mithilfe der angegebenen generischen IComparer<T>-Schnittstelle nach einem Wert.Searches an entire one-dimensional sorted array for a value using the specified IComparer<T> generic interface.
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);
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
Typparameter
- T
Der Typ der Elemente des Arrays.The type of the elements of the array.
Parameter
- array
- T[]
Das zu durchsuchende sortierte eindimensionale und nullbasierte Array.The sorted one-dimensional, zero-based Array to search.
- value
- T
Das Objekt, nach dem gesucht werden soll.The object to search for.
- comparer
- IComparer<T>
Die IComparer<T>-Implementierung, die beim Vergleich von Elementen verwendet werden soll.The IComparer<T> implementation to use when comparing elements.
- oder --or-
null
, wenn die IComparable<T>-Implementierung des jeweiligen Elements verwendet werden soll.null
to use the IComparable<T> implementation of each element.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
comparer
ist null
und value
verfügt über einen Typ, der nicht mit den Elementen von array
kompatibel ist.comparer
is null
, and value
is of a type that is not compatible with the elements of array
.
comparer
ist null
und T
implementiert die generische IComparable<T>-Schnittstelle nicht.comparer
is null
, and T
does not implement the IComparable<T> generic interface
Beispiele
Das folgende Beispiel veranschaulicht die Sort<T>(T[], IComparer<T>) generische Methoden Überladung und die BinarySearch<T>(T[], T, IComparer<T>) generische Methoden Überladung.The following example demonstrates the Sort<T>(T[], IComparer<T>) generic method overload and the BinarySearch<T>(T[], T, IComparer<T>) generic method overload.
Im Codebeispiel wird ein alternativer Vergleich für Zeichen folgen mit dem Namen ReverseCompare
definiert, der die generische Schnittstelle IComparer<string>
(IComparer(Of String)
in Visual Basic C++, IComparer<String^>
in Visual) implementiert.The code example defines an alternative comparer for strings, named ReverseCompare
, which implements the IComparer<string>
(IComparer(Of String)
in Visual Basic, IComparer<String^>
in Visual C++) generic interface. Der Vergleich Ruft die CompareTo(String)-Methode auf und kehrt die Reihenfolge der Vergleichs Elemente um, sodass die Zeichen folgen nicht zu hoch, sondern zu niedrig sortiert werden.The comparer calls the CompareTo(String) method, reversing the order of the comparands so that the strings sort high-to-low instead of low-to-high.
Das Array wird angezeigt, sortiert und erneut angezeigt.The array is displayed, sorted, and displayed again. Arrays müssen sortiert werden, um die BinarySearch-Methode zu verwenden.Arrays must be sorted in order to use the BinarySearch method.
Hinweis
Die Aufrufe der generischen Methoden Sort<T>(T[], IComparer<T>) und BinarySearch<T>(T[], T, IComparer<T>) unterscheiden sich nicht von den Aufrufen ihrer nicht generischen Entsprechungen, da Visual Basic C#,, C++ und den Typ des generischen Typparameters vom Typ des ersten Arguments ableiten.The calls to the Sort<T>(T[], IComparer<T>) and BinarySearch<T>(T[], T, IComparer<T>) generic methods do not look any different from calls to their nongeneric counterparts, because Visual Basic, C#, and C++ infer the type of the generic type parameter from the type of the first argument. Wenn Sie den Ildasm. exe (IL-Disassembler) verwenden, um die MSIL (Microsoft Intermediate Language) zu überprüfen, können Sie sehen, dass die generischen Methoden aufgerufen werden.If you use the Ildasm.exe (IL Disassembler) to examine the Microsoft intermediate language (MSIL), you can see that the generic methods are being called.
Die generische Methoden Überladung von BinarySearch<T>(T[], T, IComparer<T>) wird dann verwendet, um nach zwei Zeichen folgen zu suchen, eine, die nicht im-Array ist, und eine, die ist.The BinarySearch<T>(T[], T, IComparer<T>) generic method overload is then used to search for two strings, one that is not in the array and one that is. Das Array und der Rückgabewert der BinarySearch<T>(T[], T, IComparer<T>)-Methode werden an die generische Methode ShowWhere
übergeben, die den Indexwert anzeigt, wenn die Zeichenfolge gefunden wird, und andernfalls die Elemente, zwischen denen die Such Zeichenfolge fällt, wenn Sie sich im Array befindet.The array and the return value of the BinarySearch<T>(T[], T, IComparer<T>) method are passed to the ShowWhere
generic method, which displays the index value if the string is found, and otherwise the elements the search string would fall between if it were in the array. Der Index ist negativ, wenn die Zeichenfolge nicht n das Array ist, sodass die ShowWhere
-Methode das bitweise Komplement (der ~ C# -Operator C++in und Visual, Xor
-1 in Visual Basic) übernimmt, um den Index des ersten Elements in der Liste zu erhalten, das größer als die Such Zeichenfolge ist.The index is negative if the string is not n the array, so the ShowWhere
method takes the bitwise complement (the ~ operator in C# and Visual C++, Xor
-1 in Visual Basic) to obtain the index of the first element in the list that is larger than the search string.
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.
*/
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.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn das Array den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If the Array does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index gleich der Größe des Arrays ist, sind keine Elemente größer als value
im Array.If this index is equal to the size of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
Der Vergleich passt an, wie die Elemente verglichen werden.The comparer customizes how the elements are compared. Beispielsweise können Sie eine System.Collections.CaseInsensitiveComparer als Vergleichs Operator verwenden, um Zeichen folgen suchen ohne Berücksichtigung der Groß-/Kleinschreibung auszuführen.For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches.
Wenn comparer
nicht null
ist, werden die Elemente von array
mithilfe der angegebenen IComparer<T> Implementierung der generischen-Schnittstelle mit dem angegebenen Wert verglichen.If comparer
is not null
, the elements of array
are compared to the specified value using the specified IComparer<T> generic interface implementation. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der durch comparer
definierten Sortierreihenfolge sortiert werden. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by comparer
; otherwise, the result might be incorrect.
Wenn comparer
null
ist, erfolgt der Vergleich mithilfe der IComparable<T> generischen Schnittstellen Implementierung, die von T
bereitgestellt wird.If comparer
is null
, the comparison is done using the IComparable<T> generic interface implementation provided by T
. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable<T> Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable<T> implementation; otherwise, the result might be incorrect.
Hinweis
Wenn comparer
null
und value
die IComparable<T> generische Schnittstelle nicht implementiert, werden die Elemente von array
vor Beginn der Suche nicht auf IComparable<T> getestet.If comparer
is null
and value
does not implement the IComparable<T> generic interface, the elements of array
are not tested for IComparable<T> before the search begins. Eine-Ausnahme wird ausgelöst, wenn bei der Suche ein Element gefunden wird, das IComparable<T>nicht implementiert.An exception is thrown if the search encounters an element that does not implement IComparable<T>.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. aus diesem Grund generieren Vergleiche mit null
keine Ausnahme.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable<T> Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable<T> implementation, even if value
is null
. Das heißt, die IComparable<T>-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable<T> implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
der Length von array
ist.This method is an O(log n
) operation, where n
is the Length of array
.
Siehe auch
BinarySearch<T>(T[], Int32, Int32, T)
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der generischen IComparable<T>-Schnittstelle, die von jedem Element des Array und vom angegebenen Wert implementiert wird, nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the IComparable<T> generic interface implemented by each element of the Array and by the specified value.
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
Typparameter
- T
Der Typ der Elemente des Arrays.The type of the elements of the array.
Parameter
- array
- T[]
Das zu durchsuchende sortierte eindimensionale und nullbasierte Array.The sorted one-dimensional, zero-based Array to search.
- index
- Int32
Der Startindex des zu durchsuchenden Bereichs.The starting index of the range to search.
- length
- Int32
Die Länge des zu durchsuchenden Bereichs.The length of the range to search.
- value
- T
Das Objekt, nach dem gesucht werden soll.The object to search for.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
index
ist kleiner als die Untergrenze von array
.index
is less than the lower bound of array
.
- oder --or-
length
ist kleiner als Null.length
is less than zero.
index
und length
geben keinen gültigen Bereich im array
an.index
and length
do not specify a valid range in array
.
- oder --or-
value
verfügt über einen Typ, der nicht mit den Elementen von array
kompatibel ist.value
is of a type that is not compatible with the elements of array
.
Die generische IComparable<T>-Schnittstelle wird von T
nicht implementiert.T
does not implement the IComparable<T> generic interface.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn das Array den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If the array does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index gleich der Größe des Arrays ist, sind keine Elemente größer als value
im Array.If this index is equal to the size of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
T
müssen die IComparable<T> generische-Schnittstelle implementieren, die für Vergleiche verwendet wird.T
must implement the IComparable<T> generic interface, which is used for comparisons. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable<T> Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable<T> implementation; otherwise, the result might be incorrect.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. aus diesem Grund generieren Vergleiche mit null
keine Ausnahme.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable<T> Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable<T> implementation, even if value
is null
. Das heißt, die IComparable<T>-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable<T> implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
length
ist.This method is an O(log n
) operation, where n
is length
.
Siehe auch
BinarySearch<T>(T[], Int32, Int32, T, IComparer<T>)
Durchsucht einen Bereich von Elementen in einem sortierten eindimensionalen Array mithilfe der angegebenen generischen IComparer<T>-Schnittstelle nach einem Wert.Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer<T> generic interface.
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);
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
Typparameter
- T
Der Typ der Elemente des Arrays.The type of the elements of the array.
Parameter
- array
- T[]
Das zu durchsuchende sortierte eindimensionale und nullbasierte Array.The sorted one-dimensional, zero-based Array to search.
- index
- Int32
Der Startindex des zu durchsuchenden Bereichs.The starting index of the range to search.
- length
- Int32
Die Länge des zu durchsuchenden Bereichs.The length of the range to search.
- value
- T
Das Objekt, nach dem gesucht werden soll.The object to search for.
- comparer
- IComparer<T>
Die IComparer<T>-Implementierung, die beim Vergleich von Elementen verwendet werden soll.The IComparer<T> implementation to use when comparing elements.
- oder --or-
null
, wenn die IComparable<T>-Implementierung des jeweiligen Elements verwendet werden soll.null
to use the IComparable<T> implementation of each element.
Gibt zurück
Der Index des angegebenen value
im angegebenen array
, sofern value
gefunden wurde, andernfalls eine negative Zahl.The index of the specified value
in the specified array
, if value
is found; otherwise, a negative number. Wenn value
nicht gefunden wurde und value
kleiner als mindestens ein Element in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement des Indexes des ersten Elements, das größer als value
ist.If value
is not found and value
is less than one or more elements in array
, the negative number returned is the bitwise complement of the index of the first element that is larger than value
. Wenn value
nicht gefunden wurde und value
größer als alle Elemente in array
ist, entspricht die zurückgegebene negative Zahl dem bitweisen Komplement (des Indexes des letzten Elements plus 1).If value
is not found and value
is greater than all elements in array
, the negative number returned is the bitwise complement of (the index of the last element plus 1). Wenn diese Methode mit einem nicht sortierten array
aufgerufen wird, kann der Rückgabewert falsch sein und selbst dann eine negative Zahl zurückgegeben werden, wenn value
in array
vorhanden ist.If this method is called with a non-sorted array
, the return value can be incorrect and a negative number could be returned, even if value
is present in array
.
Ausnahmen
array
ist null
.array
is null
.
index
ist kleiner als die Untergrenze von array
.index
is less than the lower bound of array
.
- oder --or-
length
ist kleiner als Null.length
is less than zero.
index
und length
geben keinen gültigen Bereich im array
an.index
and length
do not specify a valid range in array
.
- oder --or-
comparer
ist null
, und value
verfügt über einen Typ, der nicht mit den Elementen von array
kompatibel ist.comparer
is null
, and value
is of a type that is not compatible with the elements of array
.
comparer
ist null
, und T
implementiert die generische IComparable<T>-Schnittstelle nicht.comparer
is null
, and T
does not implement the IComparable<T> generic interface.
Hinweise
Diese Methode unterstützt nicht das Suchen von Arrays, die negative Indizes enthalten.This method does not support searching arrays that contain negative indexes. array
muss sortiert werden, bevor diese Methode aufgerufen wird.array
must be sorted before calling this method.
Wenn das Array den angegebenen Wert nicht enthält, gibt die Methode eine negative Ganzzahl zurück.If the array does not contain the specified value, the method returns a negative integer. Sie können den bitweisen Komplement Operator (~ in C#, Not
in Visual Basic) auf das negative Ergebnis anwenden, um einen Index zu entwickeln.You can apply the bitwise complement operator (~ in C#, Not
in Visual Basic) to the negative result to produce an index. Wenn dieser Index gleich der Größe des Arrays ist, sind keine Elemente größer als value
im Array.If this index is equal to the size of the array, there are no elements larger than value
in the array. Andernfalls ist dies der Index des ersten Elements, das größer als value
ist.Otherwise, it is the index of the first element that is larger than value
.
Der Vergleich passt an, wie die Elemente verglichen werden.The comparer customizes how the elements are compared. Beispielsweise können Sie eine System.Collections.CaseInsensitiveComparer als Vergleichs Operator verwenden, um Zeichen folgen suchen ohne Berücksichtigung der Groß-/Kleinschreibung auszuführen.For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches.
Wenn comparer
nicht null
ist, werden die Elemente von array
mithilfe der angegebenen IComparer<T> Implementierung der generischen-Schnittstelle mit dem angegebenen Wert verglichen.If comparer
is not null
, the elements of array
are compared to the specified value using the specified IComparer<T> generic interface implementation. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der durch comparer
definierten Sortierreihenfolge sortiert werden. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by comparer
; otherwise, the result might be incorrect.
Wenn comparer
null
ist, erfolgt der Vergleich mithilfe der IComparable<T> generischen Schnittstellen Implementierung, die für den Typ T
bereitgestellt wird.If comparer
is null
, the comparison is done using the IComparable<T> generic interface implementation provided for type T
. Die Elemente array
müssen bereits in einem zunehmenden Wert entsprechend der Sortierreihenfolge sortiert werden, die durch die IComparable<T> Implementierung definiert wird. Andernfalls ist das Ergebnis möglicherweise falsch.The elements of array
must already be sorted in increasing value according to the sort order defined by the IComparable<T> implementation; otherwise, the result might be incorrect.
Doppelte Elemente sind zulässig.Duplicate elements are allowed. Wenn die Array mehr als ein Element enthält, das gleich value
ist, gibt die Methode den Index von nur einem der Vorkommen und nicht notwendigerweise der ersten zurück.If the Array contains more than one element equal to value
, the method returns the index of only one of the occurrences, and not necessarily the first one.
null
können immer mit einem beliebigen anderen Referenztyp verglichen werden. Daher generieren Vergleiche mit null
keine Ausnahme bei der Verwendung IComparable<T>.null
can always be compared with any other reference type; therefore, comparisons with null
do not generate an exception when using IComparable<T>.
Hinweis
Bei jedem getesteten Element wird value
an die entsprechende IComparable<T> Implementierung weitergeleitet, auch wenn value
null
ist.For every element tested, value
is passed to the appropriate IComparable<T> implementation, even if value
is null
. Das heißt, die IComparable<T>-Implementierung bestimmt, wie ein bestimmtes Element mit null
verglichen wird.That is, the IComparable<T> implementation determines how a given element compares to null
.
Bei dieser Methode handelt es sich um einen O (Log n
)-Vorgang, bei dem n
length
ist.This method is an O(log n
) operation, where n
is length
.