显示列表中的所有字符串

时间:2016-11-05 22:25:08

标签: c#

我正在尝试创建两个集并将它们放在一个列表中并显示列表中的所有项目。我的代码出错了。 错误:System.Collections.Generic.List 1[System.Collections.Generic.SortedSet 1 [System.String]] 我在下面附上我的代码。任何帮助表示赞赏。

    namespace Prog 5
    {
        class Program
        {        
    static void Main(string[] args)
    {
        List<SortedSet<string>> items = new List<SortedSet<string>>();
        SortedSet<string> set = new SortedSet<string>();
        SortedSet<string> set2 = new SortedSet<string>();

        set.Add("a");
        set.Add("b");
        set.Add("d");
        set.Add("c");
        set2.Add("e");
        set2.Add("d");
        set2.Add("c");
        foreach (string item in set)
        {
            items.Add(set);
        }
        foreach (string item in set2)
        {
            items.Add(set2);
        }
        DisplayItem(items);

    }

    public static void DisplaySet(SortedSet<string> set)
    {

        string set1 = string.Join(",", set);
        Console.WriteLine(set1);
        Console.ReadLine();
    }

    public static void DisplayItem(List<SortedSet<string>> items)
    {
        foreach (SortedSet<string> item in items)
        {
            Console.WriteLine(items);
            Console.ReadLine();
        }
    }
        }

3 个答案:

答案 0 :(得分:0)

SortedSet<string>继承自Object,ToString()的默认行为是GetType().ToString();,因为您在控制台中收到System.Collections.Generic.List1[System.Collections.Generic.SortedSet1[System.String]]Reference Code

public virtual String ToString()
{
    return GetType().ToString();
}

解决方案:如果您想在控制台中显示SortedSet的元素,您应该使用它:

Console.WriteLine(string.Join(",", item.ToArray()));

这将连接SortedSet中的所有字符串,并在控制台中使用,分隔符显示它们。

答案 1 :(得分:0)

在DisplayItem(...)中你有Console.WriteLine(items)......类型为List&gt;。自动调用ToString()以生成一个字符串供Console.WriteLine输出:这就是你得到当前消息的原因。我猜你想把每个 项目 写入控制台。

public static void DisplayItem(List<SortedSet<string>> items)
{
    foreach (SortedSet<string> item in items)
    {
        DisplaySet(item);
    }
}

答案 2 :(得分:0)

如果我正确理解了您的要求,您希望将2个SortedSets合并到单个列表中,或者可能设置。

以下代码应该有效(解释其工作原理如下):

class Program
{
    static void Main(string[] args)
    {
        SortedSet<string> items = new SortedSet<string>();
        SortedSet<string> set = new SortedSet<string>();
        SortedSet<string> set2 = new SortedSet<string>();

        set.Add("a");
        set.Add("b");
        set.Add("d");
        set.Add("c");
        set2.Add("e");
        set2.Add("d");
        set2.Add("c");
        foreach (string item in set)
        {
            items.Add(item);
        }
        foreach (string item in set2)
        {
            items.Add(item);
        }
        DisplayItem(items);

    }

    public static void DisplaySet(SortedSet<string> set)
    {

        string set1 = string.Join(",", set);
        Console.WriteLine(set1);
        Console.ReadLine();
    }

    public static void DisplayItem(SortedSet<string> items)
    {
        foreach (string item in items)
        {
            Console.WriteLine(item);
        }
        Console.ReadLine();
    }
}

有问题的代码中的主要问题是它正在尝试创建OrderedSet<string>个对象的列表。由于每个OrderedSet<string>代表一个字符串集合,因此您需要迭代这些集合并将字符串放入新集合中。提出的代码没有这样做。而不是它将7个OrderedSet<string>对象添加到列表中。

更正了代码修复程序,并将字符串添加到新OrderedList<string>。您可以根据您的要求决定它的集合。如果您需要收集不包含重复项并进行排序,您可以选择新的OrderedSet<string>,但如果您不关心重复项,那么您可以选择一个 List<string>

如果您希望比较OrderedSet<string>List<string>之间的差异,只需将“商品”数据类型更改为List<string>即可运行该程序。

当前输出:

a
b
c
d
e

如果你改变了

SortedSet<string> items = new SortedSet<string>();

List<string> items = new List<string>();
...
public static void DisplayItem(List<string> items)
{
...

你会得到:

a
b
c
d
c
d
e