当我在 for 循环中添加 stack.Count 时,它具有神奇的价值,即使我这样做了 < stack.Count + "WhatEverNumber" it never adds last int to array result nor raises an error.
static void Main()
{
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
int[] imput = StripStack(stack);
foreach (var item in imput)
{
Console.WriteLine(item);
}
Console.Read();
}
static int[] StripStack(Stack<int> stack)
{
int[] result = new int[stack.Count];
for (int i = 0; i < stuck.Count; i++)//here the stuck.Count is like magic
{
result[i] = stack.Pop();
}
return result;
}
我期望 stack.Count 值类似于 5,但如果您尝试向其中添加某些内容,则似乎具有最大值,例如 4。但如果我在 for 循环代码之前存储它就可以正常工作。
static void Main()
{
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
int[] imput = StripStack(stack);
foreach (var item in imput)
{
Console.WriteLine(item);
}
Console.Read();
}
static int[] StripStack(Stack<int> stack)
{
int stackBigenes = stack.Count;
int[] result = new int[stack.Count];
for (int i = 0; i < stackBigenes; i++)
{
result[i] = stack.Pop();
}
return result;
}