Dictionary支持通过键访问元素。 列表支持通过索引访问。 有没有支持通过密钥和索引访问的数据结构?支持通过索引访问的数据结构以及键
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("abc", "def");
d.Add("ghi", "ghi");
d.Add("abcd", "abcd");
d.Add("how", "howis");
foreach (KeyValuePair<string, string> kv in d)
Console.WriteLine(kv.Key);
输出为
abc
ghi
abcd
how
所以项目在同一序列出来,因为他们加入。这是否意味着字典会依次维护项目?如果是,我们可以通过索引访问它们吗?如果不是,是否有这样的可用集合支持索引访问元素以及密钥?
Dictionary中元素的顺序是不确定的,你不应该依赖它。 – 2013-05-07 13:15:53