Csharp/CSharp Tutorial/Data Structure/Array Search — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Use the BinarySearch() method to search charArray for "o"

using System;
class MainClass
{
  public static void Main()
  {
    char[] charArray = {"w", "e", "l", "c", "o", "m", "e"};
    Array.Sort(charArray);  // sort the elements        
    int index = Array.BinarySearch(charArray, "o");
    Console.WriteLine("Array.BinarySearch(charArray, "o") = " + index);
  }
}
Array.BinarySearch(charArray, "o") = 5

Use the BinarySearch() method to search intArray for the number 4: BinarySearch() returns a negative value

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, 4);
    Console.WriteLine("Array.BinarySearch(intArray, 4) = " + index);
  }
}
Array.BinarySearch(intArray, 4) = -4

Use the BinarySearch() method to search intArray for the number 5

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);
  }
}
Array.BinarySearch(intArray, 5) = 3

Use the BinarySearch() method to search stringArray for "abc345"

using System;
class MainClass
{
  public static void Main()
  {
    string[] stringArray = {"t", "i", "a", "test", "abc123", "abc345"};
    Array.Sort(stringArray);
        
    int index = Array.BinarySearch(stringArray, "abc345");
    Console.WriteLine("Array.BinarySearch(stringArray, \"abc345\") = " + index);
  }
}
Array.BinarySearch(stringArray, "abc345") = 2