2013-08-18 64 views
-1

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

+0

_these括号:“()”_是可选的。 –

回答

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

谢谢。为什么我不应该使用字节?它不超过255. – LukAss741

+0

'int'是默认(优化)类型。你需要特殊的理由来使用'byte'或'short',而不是这里的情况。 –

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

相关问题