问题描述
我有一个字典结构,里面有多个键值对。 myDict.Add(key1,value1);
myDict.Add(key2,value2)
myDict.Add(key3,value3);
我的字典用作某些控件的数据源。在控件的下拉菜单中,我看到的项目是这样的:
key1
key2
key3
订单与我的字典相同。
我知道Dictionary不像arrayList - 你可以得到索引。
我不能使用sortedDictionary。
现在我需要在我的程序的某个点添加一个键值对这个字典,我希望它与我这样做的效果相同:
myDict.Add(newKey,newValue);
myDict.Add(key1,value1);
myDict.Add(key2,value2);
myDict.Add(key3,value3);
如果我这样做,我知道newKey将在我的控制中显示为第一个元素。
我有一个想法来创建一个tempDict,将每个对的myDict放在tempDict中,然后清除myDict,然后添加对如下:
myDict.Add(newKey,newValue);
myDict.Add(key1,value1);
myDict.Add(key2,value2);
myDict.Add(key3,value3);
有更好的方法吗?
谢谢!
不订购。任何感觉到的订单维护是偶然的(以及特定实现的工件,包括但不限于桶选择顺序和计数)。
这些方法使用)我知道:
-
- .NET4,不可变,可以将键映射到多个值(在构建过程中观看重复项)
-
- 旧的,非泛型的,预期的Dictionary性能界限(其他两种方法是 forget(key)/ set(key))
- code>列表< KeyValuePair< K,V>
- .NET2 / 3可以,可变更多的脚步,可以将键映射到多个值(在插入中观察重复项)
快乐编码
创建一个维护插入顺序的哈希数据结构实际上只是一个标准的轻微修改哈希实现(Ruby哈希现在保持插入顺序);然而,这并没有在.NET中完成,更重要的是它是Dictionary / IDictionary合同的一部分。
I have a dictionary structure, with multiple key value pairs inside.
myDict.Add(key1, value1);
myDict.Add(key2, value2);
myDict.Add(key3, value3);
My dictionary is used as a data source for some control. In the control's dropdown I see the items are like this:
key1
key2
key3
The order looks identical to my dictionary.I know Dictionary is not like arrayList - you can get the index or so.I cannot use sortedDictionary.Now I need to add one more key value pair to this dictionary at some point of my program and I hope it has the same effect as I do this:
myDict.Add(newKey, newValue);
myDict.Add(key1, value1);
myDict.Add(key2, value2);
myDict.Add(key3, value3);
If I do this, I know newKey will display in my control as first element.
I have an idea to create a tempDict, put each pair in myDict to tempDict, then clear myDict, then add pairs back like this:
myDict.Add(newKey, newValue);
myDict.Add(key1, value1);
myDict.Add(key2, value2);
myDict.Add(key3, value3);
Is there better way than this?
Thanks!
Dictionary<K,V>
does not have an ordering. Any perceived order maintenance is by chance (and an artifact of a particular implementation including, but not limited to, bucket selection order and count).
These are the approaches (just using the Base Class Libraries BCL) I know about:
Lookup<K,V>
- .NET4, immutable, can map keys to multiple values (watch for duplicates during building)
OrderedDictionary
- Old, non-generic, expected Dictionary performance bounds (other two approaches are
O(n)
for "get(key)/set(key)")
- Old, non-generic, expected Dictionary performance bounds (other two approaches are
List<KeyValuePair<K,V>>
- .NET2/3 okay, mutable, more legwork, can map keys to multiple values (watch for duplicates in inserts)
Happy coding.
Creating a hash data-structure that maintains insertion order is actually only a slight modification of a standard hash implementation (Ruby hashes now maintain insertion order); however, this was not done in .NET nor, more importantly, is it part of the Dictionary/IDictionary contract.
这篇关于如何在字典中插入第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!