Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带密钥的多维数组_C#_Arrays_Dictionary_Key - Fatal编程技术网

C# 带密钥的多维数组

C# 带密钥的多维数组,c#,arrays,dictionary,key,C#,Arrays,Dictionary,Key,我有一个典型的多维数组,但我需要为每个子数组添加一个键。比如JSON 示例结构: { "0": { "1": { {1, 5, 9, 55} }, "5": { {97, 82, 5} } }, "2": { "0": { {9} },

我有一个典型的多维数组,但我需要为每个子数组添加一个键。比如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 scope=newdictionary()
{
{
0,
{
1.
{
1.
{
1.“测试”
}
}
}
}
};
textBox1.Text=scope[0][0][0];
怎么了

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


Dictionary scope=new Dictionary()

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

textBox1.Text = scope[0][0][0][0];
使用抛出一个
KeyNotFoundException
。这应该起作用:

textBox1.Text = scope[0][1][1][1];
不,当使用类似的初始值设定项调用无参数构造函数时,不需要括号

但是,我建议您改用
字典
,这样您就可以做到:

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)];
var scope=新字典
{
{
Tuple.Create(0,1,1,1),“test”
}
};
textBox1.Text=scope[Tuple.Create(0,1,1,1)];
如果使用
字典
,语法会更优雅一些:

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)];
var scope=新字典
{
{
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];
公共类MyMultiKeyDictionary:
I收集
{
私人词典;
公共字符串this[int w,int x,int y,int z]
{
获取{返回this.dict[Tuple.Create(w,x,y,z)];}
设置{this.dict[Tuple.Create(w,x,y,z)]=value;}
}
//…实现ICollection
}
var scope=新的MyMultiKeyDictionary
{
{
Tuple.Create(1,1,1),“test”
}
};
Text=scope[0,1,1,1];
但是如果你有任意的键,字典是有用的。如果您知道您的密钥都将从0变为N,那么一个简单的
字符串[]
是最简单的解决方案

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

在初始值设定项语法中,只能直接添加简单常量(如
int
string
)。您需要新的对象(字典),以便它成为:

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>>>
          {
             ...
          }
    },

};
var scope=新字典
{
{0,新词典
{
{0,新词典
...
}
}
},
{1,新词典
{
...
}
},
};
  • 在这里使用
    byte
    没有意义。需要小数值时,请始终使用
    int

这些括号:“()”是可选的。谢谢。为什么我不能使用字节?它不超过255。
int
是默认(优化)类型。使用
byte
short
需要特殊的原因,而不是这里的情况。