我不明白我在这里做错了什么。
我正在为我的二进制搜索方法收到此错误消息。
错误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(@"DataYear_1.txt");
String[] Months = File.ReadAllLines(@"DataMonth_1.txt");
String[] Days = File.ReadAllLines(@"DataDay_1.txt");
String[] Times = File.ReadAllLines(@"DataTime_1.txt");
String[] Depths = File.ReadAllLines(@"DataDepth_1.txt");
String[] Latitudes = File.ReadAllLines(@"DataLatitude_1.txt");
String[] Longitudes = File.ReadAllLines(@"DataLongitude_1.txt");
String[] Magnitudes = File.ReadAllLines(@"DataMagnitude_1.txt");
String[] Regions = File.ReadAllLines(@"DataRegion_1.txt");
String[] IRIS_IDs = File.ReadAllLines(@"DataIRIS_ID_1.txt");
String[] Timestamps = File.ReadAllLines(@"DataTimestamp_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;
我正在尝试制作一个应用程序,其中用户选择要搜索的数组,一旦选择了数组,用户将对数组进行分类,然后用户可以选择使用二进制搜索从数组中搜索特定项目。例如,如果用户在多年的数组中,他们可以输入" 2016",二进制搜索将从列表中搜索所有2016年项目并在控制台上显示它们。字符串阵列包含不同的数据类型:int,字符串,双倍。
这是一个快速的示例,介绍了您的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
,它与您在方法中的通用类型不同(。