I Hate to shout out for “bug” as they are often misuse / misprogramming… So I will ask for advise before blaming the devs for mistakes that are my own.
I use C#. I want to transfer all the elements of a queue into another one. PileP1 and PileP2 are queues. I use the Count property to find out the number of elements in the queue. The section of my code creating problem is the following:
Console.Error.WriteLine("PileP1 Count = {0}", PileP1.Count);
Console.Error.WriteLine("PileP2 Count = {0}", PileP2.Count);
Console.Error.WriteLine("CardsP1 Count = {0}", CardsP1.Count);
Console.Error.WriteLine("CardsP2 Count = {0}", CardsP2.Count);
for (int i = 0; i < PileP1.Count; i++)
{
CardsP1.Enqueue(PileP1.Dequeue());
}
for (int i = 0; i < PileP2.Count; i++)
{
CardsP1.Enqueue(PileP2.Dequeue());
}
Console.Error.WriteLine("after for");
Console.Error.WriteLine("PileP1 Count = {0}", PileP1.Count);
Console.Error.WriteLine("PileP2 Count = {0}", PileP2.Count);
Console.Error.WriteLine("CardsP1 Count = {0}", CardsP1.Count);
Console.Error.WriteLine("CardsP2 Count = {0}", CardsP2.Count);
My error out looks like the following:
[…]
PileP1 Count = 5
PileP2 Count = 5
CardsP1 Count = 24
CardsP2 Count = 18
after for
PileP1 Count = 2
PileP2 Count = 2
CardsP1 Count = 30
CardsP2 Count = 18
PileP1 & P2 Count are equal to 5 before the for loops, but those are only iterated 3 times. The second weird part is that if I “hardcode” 5 in the loop
for (int i = 0; i < 5; i++)
{
CardsP1.Enqueue(PileP1.Dequeue());
}
then I get the expected result (empty PileP queues and CardsP1 queue increased of 10.
Could someone explain this behavior not being a bug?