2017-05-06 3 views
0

ここで間違っていることを理解できません。エラーCS0411メソッド 'Program.Binary_Search(T []、T、IComparer)'の型引数が使用から推測できません

私のバイナリ検索方法でこのエラーメッセージが表示されます。

エラーCS0411方法 の型引数 'Program.Binary_Search(T []、T、たIComparer)' は使用から を推測することはできません。型引数を明示的に指定してみてください。

 //Binary search method. 
    public static void BinarySearch<T>(T[] data) 
    { 
     Binary_Search(data, Console.ReadLine(), Comparer<T>.Default); 
    } 

 //Binary search algorithm 
     public static int Binary_Search<T>(T[] data, T searchFor, IComparer<T>comparer) 
     { 
      int high, low, mid; 
      high = data.Length - 1; 
      low = 0; 
      //if the first element of the array is what I'm looking for then return that element. 
      if (data[0].Equals(searchFor)) 
       return 0; 
      //else if highest element in the array is the item im looking for then return the element. 
      else if (data[high].Equals(searchFor)) 
       return high; 
      else 
      { 
       //While low point is lower than or equal with high point set the mid point to be in the middle of the highest and lowers point. 
       while (low <= high) 
       { 
        mid = (high + low)/2; 
        //Compare mid point to the item searched, if the difference is 0 it means the item is there, return the element. 
        if (comparer.Compare(data[mid], searchFor) == 0) 
         return mid; 
        //Else if the difference between mid point and searched item is bigger than 0, the searched item must be lower than mid point, 
        //set the new high point to be current mid -1; 
        else if (comparer.Compare(data[mid], searchFor) > 0) 
         high = mid - 1; 
        //Else (Current mid point is lower than the searched item) set the new low point to be the current mid point +1. 
        else 
         low = mid + 1; 
       } 
       return -1; 
      } 
     } 

メインプログラム

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; 

配列が選択されると私は、ユーザーは、彼らが検索したい配列を選択したアプリケーションを作成しようとしていますユーザは配列をソートした後、ユーザは特定の項目を検索するオプションを取得しますバイナリ検索を使用して配列。たとえば、ユーザーがYears配列にある場合、「2016」と入力するとバイナリ検索でリストから2016項目をすべて検索し、コンソールに表示します。 String配列には、さまざまなデータ型が含まれています。int、strings、doublesここで

+1

'Console.ReadLine()'が 'T 'にマッチするのはなぜですか? – Idos

+0

@Idos私はパラメータとして配列を設定し、アプリケーションを実行するときにConsole.ReadLineを使用して配列からデータ型を検索するBinarySearchメソッドを作成したと思います。ジェネリック型の私の理解は非常に低く、Console.ReadLineの代わりに何を使うべきですか? – Ulfren

+0

'Console.ReadLine()'は 'string'を返します。' Binary_Search'メソッドに 'T'のインスタンスを与えなければなりません。もちろん、最初の引数として与える配列にマッチします。 –

答えて

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}"); 
} 

コンパイラはstringTのインスタンスを望んでいる(とではないので、あなたが見ているエラーメッセージがありますこれは、あなたが宣言したジェネリック型と同じではありません)。

+0

驚くべきこと、私はこれをもっと良く理解しています。助けをありがとう。 そして、ええ...それは見つかった要素を1つだけ印刷します、私はすべての値を出力するように私のバイナリ検索を変更する方法を探します。または、重複に適した別の検索アルゴリズムを使用することもできますか?各配列には600要素しか含まれていないので、時間の複雑さについてあまり心配する必要はありません。 – Ulfren

関連する問題