【发布时间】:2013-02-12 14:13:24
【问题描述】:
谁能帮我理解为什么下面两个for loops 的输出会产生不同的输出?
在我看来它们是相同的,但是原始 Dictionary 对象的 for loop 输出其所有 9 个成员,但 Queue 对象 for loop 仅输出前 5 个。
void test()
{
Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(0, "http://example.com/1.html");
d.Add(1, "http://example.com/2.html");
d.Add(2, "http://example.com/3.html");
d.Add(3, "http://example.com/4.html");
d.Add(4, "http://example.com/5.html");
d.Add(5, "http://example.com/6.html");
d.Add(6, "http://example.com/7.html");
d.Add(7, "http://example.com/8.html");
d.Add(8, "http://example.com/9.html");
Queue<KeyValuePair<int, string>> requestQueue = new Queue<KeyValuePair<int, string>>();
// build queue
foreach (KeyValuePair<int, string> dictionaryListItem in d)
{
requestQueue.Enqueue(dictionaryListItem);
Console.WriteLine(dictionaryListItem.Value);
}
Console.WriteLine(" ");
for (int i = 0; i < requestQueue.Count; i++)
{
Console.WriteLine(requestQueue.Peek().Value);
requestQueue.Dequeue();
}
}
【问题讨论】:
标签: c# dictionary queue