[英]Binary search with a list containing string arrays won't work
我已经制作了一个二进制搜索方法,但是当我尝试使用它时,我收到消息“无法将类型 int 隐式转换为字符串 []”。
这是我的二进制搜索方法:
private static int BinarySearch(List<string[]> myBlog, string searchedWord1)
{
int left = 0;
int right = myBlog.Count - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (searchedWord1.Equals(myBlog[mid][0]))
return mid;
else if (searchedWord1.CompareTo(myBlog[mid][0]) < 0)
right = mid - 1;
else
left = mid + 1;
}
return -1;
}
这是我在 main 方法中开始编码的内容:
case 7:
Console.WriteLine("Which title do you want to search for?");
string binarySearchInput = Console.ReadLine();
string[] searchedWord1 = BinarySearch(myBlog, binarySearchInput);
break;
当我将 binarySearchInput 作为第二个参数时,我收到错误消息:“无法将类型 int 隐式转换为字符串 []。”
我应该改变什么?
您从 function 返回一个 int,但在 searchedWord1 上声明了一个string[]
类型。
private static int BinarySearch(List<string[]> myBlog, string searchedWord1) {
...
}
...
string[] searchedWord1 = BinarySearch(myBlog, binarySearchInput);
注意searchedWord1
变量上的string[]
。 我相信只需将searchedWord1
的类型交换为int
就可以了:
int searchedWord1 = BinarySearch(myBlog, binarySearchInput);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.