【问题标题】:Multidimensional array with key带键的多维数组
【发布时间】:2013-08-18 20:27:13
【问题描述】:

我有一个典型的多维数组,但我需要为每个子数组添加一个键之类的东西。像 JSON。

一个示例结构:

{
    "0":
    {
        "1":
        {
            {1, 5, 9, 55}
        },
        "5":
        {
            {97, 82, 5}
        }
    },
    "2":
    {
        "0":
        {
            {9}
        },
        "2":
        {
            {3, 2, 2, 1, 4}
        }
    },
    "10":
    {
        "6":
        {
            {9, 10}
        },
        "7":
        {
            {0, 8, 2}
        }
    }
}

我会尝试用一个例子来解释它:

variable[0] would be equal "0"
variable[1] would be equal "2"
variable[3] would be equal "10"

variable[0][0] would be equal "1"
variable[0][1] would be equal "5"

variable[1][0] would be equal "0"
variable[1][1] would be equal "2"

variable[0][0][0] would be equal "1"
variable[0][0][1] would be equal "5"
variable[0][0][2] would be equal "9"
variable[0][0][3] would be equal "55"

variable[0][1][0] would be equal "97"
variable[0][1][1] would be equal "82"
variable[0][1][2] would be equal "5"

我可以通过使用更多变量来做到这一点,但我有很多数据可能需要在未来更改,所以我需要它们的结构类似于上面。在 C# 中执行此操作的最有效解决方案是什么?

我尝试了多维字典,但它的语法错误:

Dictionary<byte, Dictionary<byte, Dictionary<byte, Dictionary<byte, string>>>> scope = new Dictionary<byte, Dictionary<byte, Dictionary<byte, Dictionary<byte, string>>>>()
{
    {
        0,
        {
            1,
            {
                1,
                {
                    1, "test"
                }
            }
        }
    }
};
textBox1.Text = scope[0][0][0][0];

那里有什么问题?

还有一个问题。这些括号:“()”是否属于此末尾:

Dictionary&lt;byte, Dictionary&lt;byte, Dictionary&lt;byte, Dictionary&lt;byte, string&gt;&gt;&gt;&gt; scope = new Dictionary&lt;byte, Dictionary&lt;byte, Dictionary&lt;byte, Dictionary&lt;byte, string&gt;&gt;&gt;&gt;()?

【问题讨论】:

  • 这些括号:“()”是可选的。

标签: c# arrays dictionary key


【解决方案1】:

我尝试了多维字典,但它的语法错误

在初始化器语法中,您只能直接添加简单的常量(如intstring)。你需要新的对象(字典),所以它变成:

var scope = new Dictionary<byte, Dictionary<byte, Dictionary<byte, Dictionary<byte, string>>>>
{
    { 0, new Dictionary<byte, Dictionary<byte, Dictionary<byte, string>>>
          {
             { 0,  new Dictionary<byte, Dictionary<byte, string>>
                  ...
             }
          }
    },
    { 1, new Dictionary<byte, Dictionary<byte, Dictionary<byte, string>>>
          {
             ...
          }
    },

};
  • 在这里使用byte 没有意义。当您需要少量数字时,请始终使用int

【讨论】:

  • 谢谢。为什么我不应该使用字节?不超过 255。
  • int 是默认(优化)类型。使用byteshort需要特殊原因,这里不是这样。
【解决方案2】:

除了Henk Holterman 指出的语法问题之外,您还使用键 1 初始化子词典,因此:

textBox1.Text = scope[0][0][0][0];

抛出一个KeyNotFoundException。这应该有效:

textBox1.Text = scope[0][1][1][1];

不,当使用这样的初始化程序调用无参数构造函数时,您不需要括号。

但是,我建议改用Dictionary&lt;Tuple&lt;byte, byte, byte, byte&gt;, string&gt;,这样您就可以这样做:

var scope = new Dictionary<Tuple<byte, byte, byte, byte>, string>
{
    {
        Tuple.Create<byte, byte, byte, byte>(0, 1, 1, 1), "test"
    }
};

textBox1.Text = scope[Tuple.Create<byte, byte, byte, byte>(0, 1, 1, 1)];

如果你使用Dictionary&lt;Tuple&lt;int, int, int, int&gt;, string&gt;,语法会更优雅一点:

var scope = new Dictionary<Tuple<int, int, int, int>, string>
{
    {
        Tuple.Create(1, 1, 1), "test"
    }
};

textBox1.Text = scope[Tuple.Create(0, 1, 1, 1)];

或者您可以创建自己的类来包装它并提供更方便的索引器:

public class MyMultiKeyDictionary : 
    ICollection<KeyValuePair<Tuple<int, int, int, int>, string>>
{
    private Dictionary<Tuple<int, int, int, int>, string> dict;

    public string this[int w, int x, int y, int z]
    {
        get { return this.dict[Tuple.Create(w, x, y, z)]; }
        set { this.dict[Tuple.Create(w, x, y, z)] = value; }
    }

    // ... implement ICollection
}

var scope = new MyMultiKeyDictionary 
{
    {
        Tuple.Create(1, 1, 1), "test"
    }
};

textBox1.Text = scope[0, 1, 1, 1];

但是,如果您有任意键,则字典很有用。如果您知道您的密钥都将在 0 到 N 之间变化,那么简单的 string[][][] 是最简单的解决方案。

【讨论】:

    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2013-04-22
    • 2016-12-24
    相关资源
    最近更新 更多