【问题标题】:List< > Inside of Dictionary< K,List< > > (C#)List< > 字典内部< K,List< > > (C#)
【发布时间】:2017-09-10 21:04:53
【问题描述】:

这里是学生。

正在处理当前的 C# 项目。我必须创建一个扑克牌列表,以及一个 Dictionary> >。

列表是独立创建的,字典包含卡片的集合。可以从集合中添加或删除卡片。

不过,我在将卡片添加到收藏中时遇到了问题。我从用户输入中获得了列表的索引号,并试图根据索引简单地更新字典中的值。

抛出错误的行在这里。错误是 无法将类型“”隐式转换为 System.Collections.Generic.List'

我一生都无法弄清楚这意味着什么以及如何解决它..

_collections[input] = _cards[cardSelectionIndex];

这是我当前的代码块:

    public void AddToCollection()
    {
        PrintValuesCollections<string, List<Card>>(_collections);
        Console.WriteLine("Type in the name of the collection to add a card to:");
        string input = Console.ReadLine();

        bool found = _collections.ContainsKey(input);
        if (found)
        {
            Console.WriteLine("List of current cards to choose from:");

            for (int i = 0; i < _cards.Count; i++)
            {
                    Console.WriteLine("Index number:\r\n"+i+"Card Info:\r\n"+_cards[i]+"\n");
            }

            Console.WriteLine("Type in the index of the card from the list to add to the collection:");
            string cardSelection = Console.ReadLine();
            int cardSelectionIndex = 0;
            while (!int.TryParse(cardSelection, out cardSelectionIndex))
            {
                Console.Write("Please enter a number from the list above: ");
                input = Console.ReadLine();
            }

            _collections[input] = _cards[cardSelectionIndex];
            _cards.RemoveAt(cardSelectionIndex);
        }
        else
            Console.WriteLine("Collection name not found.");
    }

【问题讨论】:

  • _collections[input].Add(_cards[cardSelectionIndex]);?
  • 我不确定,但这不会添加一个新的唯一键吗?我之前应该提到过,我有已经创建的键,只是想根据用户的键输入来更新值。

标签: c# list dictionary


【解决方案1】:

我假设 _collections 是 Dictionary&lt;string, List&lt;Card&gt;&gt; 类型,_cards 是 List&lt;Card&gt;

所以这就是你想要做的:

//_collections[input] = _cards[cardSelectionIndex];
List<Card> a = _collections[input];
Card b = _cards[cardSelectionIndex];
a = b;

注意a 和b 的类型。这正确地给出了错误:“Cannot implicitly convert type '&lt; filename.CardClass &gt;' to System.Collections.Generic.List&lt; filename.CardClass &gt;'”

您可能想要做的是在该列表中添加一张卡片

List<Card> a = _collections[input];
Card b = _cards[cardSelectionIndex];
a.Add(b);

或者干脆

_collections[input].Add(_cards[cardSelectionIndex]);

【讨论】:

  • 这是有道理的。谢谢你的解释薄片!所以我不必打开另一个线程,我必须根据用户输入的键打印一个列表。我目前正在使用 for (int i = 0; i >.keys 不能像方法一样使用。这里有什么想法吗?
  • @DylanColeDuke 没问题!享受你周日剩下的时间:)
  • 如果您想打印_cards 列表,那么您应该在for 循环中使用该列表。 for(int i = 0; i &lt; _cards.Count; i++) ...
  • 关闭,但我不需要 _cards 列表。我需要从字典键打印列表。
  • 太棒了,我看到你在事先声明一个独立列表时做了什么,我试图一次做太多步骤。感谢您的帮助,希望您周日也能好好休息!
【解决方案2】:

我试着更形象地解释你的例外。

您正在创建一个Dictionary&lt;string,List&lt;Card&gt;&gt;,它只不过是一个由唯一键访问的KeyValuePair&lt;string,List&lt;Card&gt;&gt; 列表。这意味着字典中的每个条目都是由字符串类型的键索引的卡片列表。

例如:

Key | Value
"hearts" | List<Card> {1, 2, 7, 8, Queen}
"spades" | List<Card> {King, Ace}

现在你的例外是在告诉你,你试图用一张牌覆盖一副牌。

这就是你要做的:

List<Card> spades = collection["spades"]; // King, Ace
spades = new Card(Queen); // Your Exception

但这是您可能想要做的:

List<Card> spades = collection["spades"]; // King, Ace
spades.Add( Card(Queen) ); // Add an element to the List

现在你的字典看起来像这样:

Key | Value
"hearts" | List<Card> {1, 2, 7, 8, Queen}
"spades" | List<Card> {King, Ace, Queen}

如果您想在您的收藏中添加一副“俱乐部”,您可以致电:

collection.Add( "clubs", new List<Card> {1, 2, 3, Ace} );

或简称:

collection["clubs"] = new List<Card> {1, 2, 3, Ace};

但是要小心,如果键已经在字典中,第一个可能会引发重复键异常,如果它不存在或覆盖现有条目,第二个只会创建一个新条目。

【讨论】:

  • 好帖子!万一 unique key 对所有人都不是很清楚......请务必注意,所有字典都可以由键值对列表表示,但并非所有键值对列表都可以是字典.字典需要从域到范围的唯一或零对应关系。 key-&gt;value。一个普通的键值对列表可以有每个域值的多个对应关系。这种情况下的键不能保证是唯一的。
  • @flakes 你是绝对正确的。感谢您的意见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 2021-10-08
  • 2015-01-14
  • 2017-01-28
  • 1970-01-01
  • 2011-03-10
相关资源
最近更新 更多