本文整理汇总了C#中System.Array.BinarySearch方法的典型用法代码示例。如果您正苦于以下问题:C# Array.BinarySearch方法的具体用法?C# Array.BinarySearch怎么用?C# Array.BinarySearch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Array
的用法示例。
在下文中一共展示了Array.BinarySearch方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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();
}
}
输出:
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.
示例2: Main
//引入命名空间
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);
}
}
}
输出:
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.
示例3: Compare
//引入命名空间
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);
}
}
}
输出:
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.
示例4: Main
//引入命名空间
using System;
class MainClass
{
public static void Main()
{
int[] intArray = {5, 2, 3, 1, 6, 9, 7, 14, 25};
Array.Sort(intArray);
int index = Array.BinarySearch(intArray, 5);
Console.WriteLine("Array.BinarySearch(intArray, 5) = " + index);
}
}