带键的多维数组

本文关键字:数组 | 更新日期: 2023-09-27 18:28:18

我有一个典型的多维数组,但我需要为每个子数组添加一个键之类的东西。像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<byte, Dictionary<byte, Dictionary<byte, Dictionary<byte, string>>>> scope = new Dictionary<byte, Dictionary<byte, Dictionary<byte, Dictionary<byte, string>>>>()

带键的多维数组

我试过多维词典,但它的语法是错误的

在初始值设定项语法中,您只能直接添加简单的常量(如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>>>
          {
             ...
          }
    },
};
  • 在这里使用CCD_ 4没有意义。当您需要一个小数字时,请始终使用int

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

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

用投掷KeyNotFoundException。这应该有效:

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

不,当使用类似的初始化器调用无参数构造函数时,不需要括号。

然而,我建议使用Dictionary<Tuple<byte, byte, byte, byte>, string>,这样你就可以这样做:

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<Tuple<int, int, int, int>, string>,语法会更优雅一点:

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[][][]是最简单的解决方案。