Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中Dictionary和Queue的输出之间的差异_C#_Dictionary_Queue - Fatal编程技术网

C# C中Dictionary和Queue的输出之间的差异

C# C中Dictionary和Queue的输出之间的差异,c#,dictionary,queue,C#,Dictionary,Queue,有人能帮我理解为什么下面两个for循环的输出会产生不同的输出吗 在我看来,它们是相同的,但是原始Dictionary对象的for循环输出其所有9个成员,但是队列对象for循环只输出前5个 void test() { Dictionary<int, string> d = new Dictionary<int, string>(); d.Add(0, "http://example.com/1.html"); d.Add(1

有人能帮我理解为什么下面两个for循环的输出会产生不同的输出吗

在我看来,它们是相同的,但是原始Dictionary对象的for循环输出其所有9个成员,但是队列对象for循环只输出前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();
        }
}

您需要在循环之前保存计数:

var count = requestQueue.Count;
for (int i = 0; i < count; i++)
{
    Console.WriteLine(requestQueue.Peek().Value);
    requestQueue.Dequeue();
}
原因是在for循环的每次迭代中都会对其进行评估:

在第一次迭代开始时,requestQueue.Count是9,i是0。 第二次迭代:requestQueue。计数是8,i是1。 第三次迭代:requestQueue。计数是7,i是2。 第四次迭代:requestQueue。计数是6,i是3。 第五次迭代:requestQueue。计数是5,i是4。 第六次迭代:requestQueue。计数为4,i为5。->退出循环


注意:队列的计数随着每次迭代而减少,因为queue.Dequeue会删除队列中的第一项并将其返回给调用者。

您需要在循环之前保存计数:

var count = requestQueue.Count;
for (int i = 0; i < count; i++)
{
    Console.WriteLine(requestQueue.Peek().Value);
    requestQueue.Dequeue();
}
原因是在for循环的每次迭代中都会对其进行评估:

在第一次迭代开始时,requestQueue.Count是9,i是0。 第二次迭代:requestQueue。计数是8,i是1。 第三次迭代:requestQueue。计数是7,i是2。 第四次迭代:requestQueue。计数是6,i是3。 第五次迭代:requestQueue。计数是5,i是4。 第六次迭代:requestQueue。计数为4,i为5。->退出循环


注意:每次迭代都会减少队列的计数,因为queue.Dequeue会删除队列中的第一个项目并将其返回给调用者。

您可能希望使用此while循环,这在此处更有意义:

while (requestQueue.Count > 0)
{
    Console.WriteLine(requestQueue.Peek().Value);
    requestQueue.Dequeue();
}
for循环的问题是删除了第一项,这也减少了Count属性。这就是它中途停止的原因


循环变量i仍在增加,而计数却在减少。

您可能希望使用此while循环,这在此处更有意义:

while (requestQueue.Count > 0)
{
    Console.WriteLine(requestQueue.Peek().Value);
    requestQueue.Dequeue();
}
for循环的问题是删除了第一项,这也减少了Count属性。这就是它中途停止的原因


循环变量i仍在增加,而计数在减少。

因为每次调用requestQueue.Dequeue时,您都在更改requestQueue中的项目数量。而是将count的值存储在本地循环中,并将该本地值作为上限。

因为每次调用requestQueue.Dequeue时,您都在更改requestQueue中的项目数量。而是将count的值存储在一个局部循环中,并将该局部循环作为上限。

spot on。谢谢为什么需要这样做?会认为requestQueue.Count会包含正确的val吗?@RodgersandHammertime:它确实包含正确的值。请再次核对我的答案。您知道“出列”会立即从队列中删除项目吗。谢谢为什么需要这样做?会认为requestQueue.Count会包含正确的val吗?@RodgersandHammertime:它确实包含正确的值。请再次核对我的答案。您是否意识到“出列”会从队列中删除该项目?@Rodgers and Hammertime:如果出于某种原因,您需要索引,您还可以将for循环更改为for int i=0;requestQueue.Count>0;i++@Rodgers和Hammertime:如果出于某种原因需要索引,还可以将for循环更改为for int i=0;requestQueue.Count>0;我++