列表中的字符串[]导致问题

时间:2016-07-04 12:44:55

标签: c# arrays list search foreach

我正在创建一个由列表中的数组组成的日志,该数组是每个新条目,列表是日志。以下是我到目前为止试图解决的问题:

using System;
using System.Collections.Generic; 
using System.Linq;                

namespace Journal
{
    class Program
    {
        static void Main(string[] args)
        {
            int menuChoice = 0;
            List<string[]> journal = new List<string[]>();

            while (menuChoice != 4)
            {
                Console.WriteLine("\n\t\t===Journal===");
                Console.WriteLine("\t[1] New entry in the journal");
                Console.WriteLine("\t[2] Search entry in the journal");
                Console.WriteLine("\t[3] Show contents of the journal");
                Console.WriteLine("\t[4] Exit");
                Console.Write("\tChoose: ");

                int.TryParse(Console.ReadLine(), out menuChoice);
                {
                    switch (menuChoice)
                    {
                        case 1:
                            {
                                string[] entry = new string[3];
                                DateTime time = DateTime.Now;
                                entry[0] = Convert.ToString(time);
                                Console.Write("\n\tWrite your title: ");
                                entry[1] = Console.ReadLine();
                                Console.Write("\n\tWrite your new entry: ");
                                entry[2] = Console.ReadLine();
                                journal.Add(entry);
                            }
                            break;
                        case 2:
                            Console.Write("\n\tSearch entry in the journal: ");
                            string searchTerm = Console.ReadLine();
                            for (int i = 0; i < journal.Count; i++)

                                if (journal[i].Contains(searchTerm))
                                {
                                    Console.WriteLine(journal[i]);
                                }
                                else
                                {
                                    Console.WriteLine("\n\tYour search was not found.");
                                }
                            break;
                        case 3:
                            Console.WriteLine("\n\tJournal:");
                            foreach (string[] item in journal)
                                Console.WriteLine("\n\t" + item);
                            break;
                        case 4:
                            break;
                    }
                }
            }
        }
    }
}

我在尝试完成这项工作时遇到了很多麻烦,但它仍然没有。我想要做的是在数组中使用3个索引空间作为时间,标题和文本,然后将这3个组合放入列表中,这样它们就成为列表中的单个元素,所以当我搜索标题时他们是一群人。

我在声明日志时尝试使用普通的字符串列表但是我无法将数组添加到其中,而无需指定要插入的索引。当我将列表的类型更改为字符串[]时,foreach循环停止工作,因为它们无法将字符串[]转换为字符串,因此我将foreach循环内的字符串更改为字符串[]和现在,当我尝试将所有内容写出来或搜索时,我得到的就是&#34; System.String []&#34;。

这就是我现在所处的位置,所以如果有人能告诉我我做错了什么或告诉我如何解决这个问题,我们将不胜感激。

3 个答案:

答案 0 :(得分:0)

journal是一个字符串数组列表。 journal[i]是一个字符串数组。要打印出字符串数组中的值,需要遍历该数组。

if (journal[i].Contains(searchTerm))
{
     for (int j = 0; j < journal[i].Count; j++)
         Console.WriteLine(journal[i][j]);
}

foreach

foreach(string item in journal[i])
    Console.WriteLine(item);

答案 1 :(得分:0)

使用linq,您可以从子列表列表中生成一个List:

foreach(var item in journal.SelectMany(x => x))
{
   // item is string
}

当然,您需要添加using System.Linq;

答案 2 :(得分:0)

解决问题的最简单方法是使用JournalEntryTitleDate属性创建Body类。在该类中,您可以覆盖ToString以生成格式化输出。

如果不创建类,解决此问题的最简单方法是明智地应用少量LINQ。您还可以使用String.Join将字符串数组合并为Console.WriteLine的单个字符串。

要搜索包含搜索词的标题的第一个条目:

var selected = journal.FirstOrDefault(item => item[1].Contains(searchTerm));
if (selected != null)
{
    // selected is the first string[] with a title containing the search term.
    Console.WriteLine(String.Join("\r\n", result));
}

要搜索包含搜索字词标题的所有条目:

var selected = journal.Where(item => item[1].Contains(searchTerm));
foreach (var result in selected)
{
   // result is the string[] journal entry
   Console.WriteLine(String.Join("\r\n", result));
}