错误CS0411方法的类型参数' Program.Binary_Search(T [],T,IComparer)'无法从使用中推断出来

时间:2017-05-06 14:21:23

标签: c# algorithm interface binary-search

我不明白我在这里做错了什么。

我收到二进制搜索方法的错误消息。

  

错误CS0411方法的类型参数   ' Program.Binary_Search(T [],T,IComparer)'无法推断   从用法。尝试明确指定类型参数。

  static void Main(string[] args)
            {
                Console.WriteLine("Analysis of Seismic Data.\n");
                //Read in the files and put them into arrays.
                String[] Years = File.ReadAllLines(@"Data\Year_1.txt");
                String[] Months = File.ReadAllLines(@"Data\Month_1.txt");
                String[] Days = File.ReadAllLines(@"Data\Day_1.txt");
                String[] Times = File.ReadAllLines(@"Data\Time_1.txt");
                String[] Depths = File.ReadAllLines(@"Data\Depth_1.txt");
                String[] Latitudes = File.ReadAllLines(@"Data\Latitude_1.txt");
                String[] Longitudes = File.ReadAllLines(@"Data\Longitude_1.txt");
                String[] Magnitudes = File.ReadAllLines(@"Data\Magnitude_1.txt");
                String[] Regions = File.ReadAllLines(@"Data\Region_1.txt");
                String[] IRIS_IDs = File.ReadAllLines(@"Data\IRIS_ID_1.txt");
                String[] Timestamps = File.ReadAllLines(@"Data\Timestamp_1.txt");
        //Read in user's decision.
        Console.Write("Select which Collection is to be analysed: ");

        int collectionDecision = Convert.ToInt32(Console.ReadLine());

        //Selected which collection is to be analyzed
        switch (collectionDecision)
        {
            case 1:
                //Create another switch statement to select either ascending or descending sort.
                Console.WriteLine("\nWould you like to sort the Collection in Ascending or Descending order?");
                Console.WriteLine("1-Ascending");
                Console.WriteLine("2-Descending");
                int sortingDecision = Convert.ToInt32(Console.ReadLine());
                switch (sortingDecision)
                {
                    case 1:
                        //Using the default QuickSort option to sort the collection in an ascending order.
                        QuickSort(Years);
                        Console.WriteLine("Contents of the Ascending Year Collection: ");
                        foreach (var year in Years)
                        {
                            Console.WriteLine(year);
                        }
                        Console.WriteLine("\nEnter the Number/Name of the items you are looking for from the previously selected Collection.");
                        Console.Write("Search: ");
                        //How do I implement Binary Search here to search for specific items from a selected Array/Collection and display them?

                        break;
                    case 2:
                        //Using Comparer<T>.Create to create a custom object and change the algorithm to sort in a Descending order.
                        QuickSort(Years, Comparer<string>.Create((a, b) => b.CompareTo(a)));
                        Console.WriteLine("Contents of the Descending Year Collection: ");
                        foreach (var year in Years)
                        {
                            Console.WriteLine(year);
                        }
                        Console.WriteLine("\nEnter the Number/Name of the items you are looking for from the previously selected Collection.");
                        Console.Write("Search: ");
                        //How do I implement Binary Search here to search for specific items from a selected Array/Collection and display them?
                        break;

                }
                break;
bfg --delete-files YOUR-FILE

主程序

{
  "type": "service_account",
  "project_id": "uityityiuy",
  "private_key_id": "hjklhkkkkkkkkkkh",
  "private_key": "sdfggfdsgsdf",
  "client_email": "asdfas",
  "client_id": "asdfasasd",
  "auth_uri": "qrwqerweq",
  "token_uri": "qrwerq",
  "auth_provider_x509_cert_url": "eryter",
  "client_x509_cert_url": "ertyertytter"
}

我正在尝试创建一个应用程序,用户选择要搜索的数组,一旦选择了数组,用户就会对数组进行排序,之后用户可以选择使用二进制搜索从数组中搜索特定项目。例如,如果用户在Years数组中,他们可以输入&#34; 2016&#34;二进制搜索将搜索列表中的所有2016项,并在控制台上显示它们。 String数组包含不同的数据类型:整数,字符串,双精度。

1 个答案:

答案 0 :(得分:0)

这是一个关于你的Binary_Search方法如何使用字符串的快速例子:

public static void Main(string[] args)
{
    Console.WriteLine("Please enter a value to search:");

    var toSearch = Console.ReadLine();

    var strings = GetOrderedArrayOfStrings(); // assuming here we have an ordered string[]

    var position = Binary_Search(strings, toSearch, Comparer<string>.Default);

    if(position == -1)
        Console.WriteLine("Not found");
    else
        Console.WriteLine($"Found at position {position}");
}

您看到的错误消息是因为编译器需要一个T实例(而不是string,这与您声明的方法中的泛型类型不同。) p>