繁体   English   中英

使用包含字符串 arrays 的列表进行二进制搜索将不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM