ここで間違っていることを理解できません。エラー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ここで
'Console.ReadLine()'が 'T 'にマッチするのはなぜですか? – Idos
@Idos私はパラメータとして配列を設定し、アプリケーションを実行するときにConsole.ReadLineを使用して配列からデータ型を検索するBinarySearchメソッドを作成したと思います。ジェネリック型の私の理解は非常に低く、Console.ReadLineの代わりに何を使うべきですか? – Ulfren
'Console.ReadLine()'は 'string'を返します。' Binary_Search'メソッドに 'T'のインスタンスを与えなければなりません。もちろん、最初の引数として与える配列にマッチします。 –