【问题标题】:Adding Data in List inside a Dictionary in C#在 C# 中的字典内的列表中添加数据
【发布时间】:2021-10-22 21:33:33
【问题描述】:
  • 在第 1 步中,我编写了这段代码来访问预先存在的列表并在其中添加价值。
  • 在第 2 步中,我使用新列表更新了字典。
  • 再次在第 3 步中,我必须访问字典中的列表才能打印结果。

是否有任何过程或快捷方式可以直接在字典中向这个预先存在的列表添加新值而不更新它?

只需要在 Main 中编写代码。 Rest 在编译器中是硬编码的,无法更改。 您的帮助将不胜感激。欢迎提出建议:)

using System;
using System.Collections.Generic;

namespace AddNewMember             
{
    public class Club         
    {

        static Dictionary<int, string> groupInfo = new Dictionary<int, string>() { { 1, "Gold" }, { 2, "Silver" }, { 3, "Platinum" } };
        static Dictionary<int, List<String>> memberInfo = new Dictionary<int, List<String>>() {
                                    { 1, new List<string>(){ "Tom","Harry"} },
                                    { 2,new List<string>(){ "Sam","Peter"} },
                                    { 3,new List<string>(){ "Kim","Robert"} } };

        public static void Main(string[] args)        
        {
        //Write your code here. Above part is hardcoded can't be changed
            Console.WriteLine("Group Name :");
            string gName = Console.ReadLine();
            int num = 0;

            foreach (KeyValuePair<int, string> VARIABLE in groupInfo)
            {
                if (VARIABLE.Value == gName)
                {
                    num = VARIABLE.Key;
                }
            }

            Console.WriteLine("Member Name:");
            string name = Console.ReadLine();


        //Step 1
            List<string> l = memberInfo[num];
            l.Add(name);

        //Step 2
            memberInfo[num] = l;

       //Step 3
            List<string> r = memberInfo[num];
            foreach (var VARIABLE in r)
            {
                Console.WriteLine(VARIABLE);
            }

        }
    }
}

【问题讨论】:

    标签: c# list dictionary collections generic-collections


    【解决方案1】:

    在我看来,您对字典的理解是颠倒的。您使用密钥来检索值,而不是相反。如果您希望用户输入组名(金、银、铜),然后输入要添加到该组的人的姓名,您应该让字典将字符串(组名)映射到成员列表

    static Dictionary<string, List<String>> groupInfo = new() { 
      { "Gold", new(){ "Tom","Harry" } },
      { "Silver", new(){ "Sam","Peter"} },
      { "Platinum", new(){ "Kim","Robert"} }
    };
    
    public static void Main(string[] args)        
    {
        Console.WriteLine("Group Name :");
        string gName = Console.ReadLine();
            
        Console.WriteLine("Member Name :");
        string mName = Console.ReadLine();
            
        groupInfo[gName].Add(mName);
    
    }
    

    是的,就是这样。 GroupInfo 将字符串组名称映射到字符串成员名称列表,调用 groupInfo[gName] 解析为字符串列表,因此在列表上执行 Add 方法调用并添加给定的成员名称

    旁注,我正在利用最近的 c# 工具,您不必在 = 的两侧重复类型名称。编译器将知道 groupInfo 是 Dictionary&lt;string, List&lt;string&gt;&gt; 并且当它在初始化程序中出现 new() 时,它知道我的意思是 new List&lt;string&gt; 这真的可以帮助整理事情。括号是必要的,否则它会认为我试图创建一个匿名类型,这是另一回事。如果您遇到编译器错误,如果您的 c# 较旧,则可能必须恢复类型名称

    【讨论】:

      【解决方案2】:

      我们不需要将修改后的列表重新分配到字典值中。步骤#2 是多余的。当您从步骤 #1 中检索列表时。它返回一个指向字典中列表的指针(引用)。这意味着,当您将一个项目插入列表变量时,字典中的列表会更新(添加新项目)。

      另外,在第 3 步中,您获得了 r 但未使用。

      【讨论】:

        猜你喜欢
        • 2019-12-14
        • 2018-08-27
        • 1970-01-01
        • 2023-01-15
        • 2023-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        相关资源
        最近更新 更多