OrderedDictionary和字典 [英] OrderedDictionary and Dictionary

查看:80
本文介绍了OrderedDictionary和字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方式让我的词典枚举其 KeyValuePair <​​/ code>以相同的顺序他们已添加。现在,字典的doc 明确说明:

I was looking for a way to have my Dictionary enumerate its KeyValuePair in the same order that they were added. Now, Dictionary's doc clearly state that:

有关枚举的目的,在字典中的每个项目被视为 KeyValuePair< TKEY的,TValue> 表示值及其关键结构。在返回的项目的顺序是不确定的。

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<TKey, TValue> structure representing a value and its key. The order in which the items are returned is undefined.

我发现了什么,我需要的是一个 OrderedDictionary ,但作为怀疑论者,我,我决定自己试试吧:

I found out that what I needed was an OrderedDictionary, but being the sceptic that I am, I decided to try it myself:

OrderedDictionary od = new OrderedDictionary();
Dictionary<String, String> d = new Dictionary<String, String>();

for (int i = 0; i < 10; i++ )
{
    od.Add("key"+i,"value"+i);
    d.Add("key"+i,"value"+i);
}

System.Console.WriteLine("OrderedDictionary");
foreach (DictionaryEntry de in od) {
    System.Console.WriteLine(de.Key +", " +de.Value);
}

System.Console.WriteLine("Dictionary");
foreach (var tmp in d) {
    System.Console.WriteLine(tmp.Key +", " + tmp.Value);
}



输出:

Output:

OrderedDictionary
key0, value0
key1, value1
key2, value2
...

Dictionary
key0, value0
key1, value1
key2, value2
...

正如你所看到的,是有序的,并且提高2个问题:结果
在这种情况下,做了词典给不同的顺序所述一个中,所述值被添加?
我的第一个的foreach 循环保证我找回我的 KeyValuePair <​​/ code>以相同的顺序,或者我有使用索引?

As you can see, both are ordered, and that raise 2 questions:
In which case does the Dictionary give a different order that the one in which the values are added ? Does my first foreach loop assure me to retrieve my KeyValuePair in the same order, or do I have to use the index ?

推荐答案

您这样做是错误的。你不仅需要插入值依次进入词典,还能去除一些元素,看看如何才能在这之后发生了变化。接下来的代码演示了这一点:

You are doing it wrong. You need not only to insert values sequentially into dictionary, but also remove some elements and see how order has changed after this. Next code demonstrates this:

OrderedDictionary od = new OrderedDictionary();
Dictionary<String, String> d = new Dictionary<String, String>();
Random r = new Random();

for (int i = 0; i < 10; i++)
{
    od.Add("key"+i,"value"+i);
    d.Add("key"+i,"value"+i);
    if(i % 3 == 0)
    {
        od.Remove("key"+r.Next(d.Count));
        d.Remove("key"+r.Next(d.Count));
    }
}

System.Console.WriteLine("OrderedDictionary");
foreach (DictionaryEntry de in od) {
    System.Console.WriteLine(de.Key +", " +de.Value);
}

System.Console.WriteLine("Dictionary");
foreach (var tmp in d) {
    System.Console.WriteLine(tmp.Key +", " + tmp.Value);
}



打印类似(OrderedDictionary总是订购)的内容:

prints something similar to (OrderedDictionary is always ordered):

OrderedDictionary
key3, value3
key5, value5
key6, value6
key7, value7
key8, value8
key9, value9
Dictionary
key7, value7
key4, value4
key3, value3
key5, value5
key6, value6
key8, value8
key9, value9

这篇关于OrderedDictionary和字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆