【问题标题】:How to create dictionary within list如何在列表中创建字典
【发布时间】:2021-12-06 05:22:11
【问题描述】:

我有一个 Mobile 类,其中声明了我的字典。我有另一个类 Cell 我想通过它调用这个字典并向其中添加项目。我在 Mobile 类中创建了一个函数来将元素添加到字典中。我的两个班是。

public class Mobile {

   public Dictionary<string, Mobile> dict;
   
   
   public Mobile(int x,int y) {
    this.x=x;
    this.y=y;
    dict = new Dictionary<string, Mobile>();
   }
   
   pulic void Add_data_to_dictionary(string a,Mobile b)
   {
   this.dict.Add(a,b);
   }
 
}

public class Cell
{
Mobile x=new Mobile():

}

我需要在 Cell 类中实现我的代码。我的问题是我必须创建一个 nodesMobile 列表,其中每个节点必须包含一个 dictionary 并且每个字典应该有多个 键值对对。

层次是这样的

node1->dictionary[key1,val1,key2,val2] 
node2->dictionary[key1,val1,key2,val2] 
node3->dictionary[key1,val1,key2,val2] 

谁能帮我怎么做?

【问题讨论】:

  • 你的节点是什么?你想要List&lt;Cell&gt; 还是List&lt;Mobile&gt;?还有你在Mobile类中的字典是private,所以你可以通过Add_data_to_dictionary方法给它添加KeyValuePairs,但是不能从中读取数据。
  • 代码中的a,b,x, & y 是什么?它们根本没有在任何地方定义。
  • @Auditive 我的节点是列表。我也可以公开。没问题
  • 好吧,答案肯定就是你想要的this.dict.Add(a,b); - 我看不出你要解决的问题是什么?!
  • 所以给Cell一个List&lt;Mobile&gt;,你可以通过索引访问它。 Mobile 里面已经有字典了。您似乎拥有所有的构建块,但不知道如何将它们组合在一起。

标签: c# list class dictionary object


【解决方案1】:

我仍然不确定你想要实现什么,但正如你所说:

我需要一个移动类列表。在 List 的每个索引处,我需要一个 Mobile 类字典,其中每个字典可以有许多条目 键值对。

大概可以这样做:

class Program
{
    static void Main(string[] args)
    {
        // Initialize List of your Mobiles
        List<Mobile> mobiles = new List<Mobile>();

        // Fill list in some way
        for (int i = 0; i < 5; i++)
        {
            // Create new Mobile
            Mobile mobile = new Mobile();

            // Add data to its Dictionary
            mobile.AddDataToDictionary("SomeKey #" + i, mobile); // Add itself as value?!

            // Add to list of Mobiles (your nodes)
            mobiles.Add(mobile);
        }

        // Create Cell instance and pass list of Mobiles to it
        Cell cell = new Cell(mobiles);
        // Do what you want with mobiles there
        cell.DoWorkWithMobiles();

        Console.ReadKey();
    }
}


public class Mobile
{
    // Make Dictionary as public property to grant access to it for read purposes
    public Dictionary<string, Mobile> Dict { get; private set; }

    // I removed arguments x/y for this example
    public Mobile()
    {
        Dict = new Dictionary<string, Mobile>();
    }

    public void AddDataToDictionary(string key, Mobile value)
    {
        Dict.Add(key, value);
    }
}

public class Cell
{
    // To store list, which was passed to class constructor
    private List<Mobile> mobiles;

    public Cell(List<Mobile> mobiles)
    {
        this.mobiles = mobiles;
    }

    public void DoWorkWithMobiles()
    {
        // Very unclear what to do here
        for (int i = 0; i < mobiles.Count; i++)
        {
            Mobile mobile = mobiles[i];
            Mobile mobileFromDict = mobile.Dict["SomeKey #" + i];
            //or 
            mobile.Dict.TryGetValue("SomeKey #" + i, out Mobile mob);
            Mobile m = mob.Dict["SomeKey #" + i];
        }
    }
}

如您所见,您的项目的架构和逻辑不清楚,示例可能不正确且愚蠢。

【讨论】:

    猜你喜欢
    • 2019-05-19
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多