【问题标题】:Linear search in a list array列表数组中的线性搜索
【发布时间】:2021-12-14 09:56:37
【问题描述】:

在我正在制作一个“博客”的项目中工作,该项目将博客文章放入一个字符串数组,然后再传输到一个列表数组。这是我当前的代码,我将不胜感激。我对 c# 和整个编程还是很陌生,所以不确定如何解决它。

当前的问题是,在案例 3 中,我收到错误消息:运算符 '==' 不能应用于 'string' 和 'string[]' 类型的操作数,并且名称 "i" 在当前不存在“帖子在博客中”代码块的上下文。

我在这里创建了一个最小的可重现示例,该程序唯一的其他功能是 1. 删除所有博客文章和 2. 打印所有博客文章及其名称和内容。

        bool minBool = true;
        List<string[]> blogPost = new List<string[]> { };
        string[] post = new string[2];

        while (minBool)
        {
            Console.WriteLine("\n\n\tWelcome to the blog!");
            Console.WriteLine("\n\t[1] - Write a blogpost");
            Console.WriteLine("\t[3] - Search for a blogpost");
            Console.Write("\n\tChoice:");
            Int32.TryParse(Console.ReadLine(), out int input);

            switch (input)
            {
                case 1:
                    Console.Write("\tName your post: ");
                    post = new string[2];
                    post[0] = Console.ReadLine();
                    Console.Write("\tWrite your post: ");
                    post[1] = Console.ReadLine();
                    blogPost.Add(post);

                    break;

                case 3:

                    string searchTerm = Console.ReadLine();
                    string result = "The blogpost doesn't exist";
                    foreach (string blog in blogPost)
                    {
                        if (searchTerm == blog)
                        {
                            result = $"\tThe post is in the blog: {post[i]}";
                            break;
                        }
                    }
                    Console.WriteLine(result);
                    break;

【问题讨论】:

    标签: c# arrays


    【解决方案1】:

    问题在于您试图将searchterm 变量(string)与blogpost 变量(类型为string[])的项目进行比较。这行不通。
    相反,您可以这样做:

    foreach(var blog in blogPost)
    {
       if(searchTerm == blog[0] || searchTerm == blog[1])
       {
          result = $"\tThe post is in the blog: {blog[0]}";   // Print blog name
          break;
       }
    }
    

    但这样您的搜索词必须与帖子的名称或内容完全相同。快速搜索不是很方便。您可以使用Contains() 方法,而不是完全比较字符串:

    if(blog[0].Contains(searchTerm) || blog[1].Contains(searchTerm))
    {
       result = $"\tThe post is in the blog: {blog[0]}";   // Print blog name
       break;
    }
    

    现在您只需提供部分名称或内容即可搜索博客文章。
    请注意,只有满足搜索条件的第一篇博文才会打印到控制台。

    考虑创建一个BlogPost 类以将博客文章保存在List 中。这样你的代码就更有表现力,也更容易处理:

    public class BlogPost
    {
       public string Name { get; }
       public string Content { get; }
    
       public BlogPost(string name, string content)
       {
          Name = name;
          Content = content;
       }
    }
    

    然后这样保存:

    private List<BlogPost> _blogPosts = new List<BlogPost>();
    
    [...]
    
    case 1:
       Console.Write("\tName your post: ");
       var name = Console.ReadLine();
       Console.Write("\tWrite your post: ");
       var content = Console.ReadLine();
     
       _blogPosts.Add(new BlogPost(name, content));
    

    现在您的搜索看起来像这样:

    foreach (var blog in _blogPosts)
    {
       if(blog.Name.Contains(searchTerm) || blog.Content.Contains(searchTerm))
       {
          result = $"\tThe post is in the blog: {blog.Name}";   // Print blog name
          break;
       }
    }
    

    通过博客文章课程,您现在变得更加灵活。例如,您可以为搜索博客文章等时应使用的关键字添加附加属性。

    【讨论】:

    • 谢谢,但我不应该在这个项目中使用 Linq / 创建一个类,我可能应该包括它。我的错。但是,对于您发布的第一个代码块,我仍然收到“无法将类型“字符串 []”转换为字符串”错误消息。而且这种方法不是只搜索最近的帖子,而不是列表中的所有帖子吗?
    • 我编辑了我的答案以调整 foreach 循环。问题是博客的类型是string[] 而不是string。要解决此问题,请使用var(如我的回答)或string[] 类型为blog。此方法将搜索整个blogPost 列表,并在其中一篇博文与搜索词匹配时立即中断。
    • 现在可以使用了,非常感谢老兄。我真的很感激:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多