带键的多维数组

问题描述 投票:-1回答:2

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

c# arrays dictionary key
2个回答
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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.