谁能帮我理解为什么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();
}
}