异常说堆栈是空的,当它不是?

时间:2015-02-05 21:47:47

标签: c# .net stack

我在书中了解了一些advanced collections等,并且遇到了stacks。我得到了这个概念,但想制作一个快速程序,从defined point in the stack然后places all the values back onto the stack删除一个项目。我在这里有我的代码,但我得到System.InvalidOperationException类型的异常,其中堆栈的其他信息为空。我似乎无法理解;有人可以帮忙吗?

这是我的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackRemover
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 0; //the index they want to remove
            Stack[] array = new Stack[1]; // will hold the array returned by remove()
            Stack stack = new Stack();

            //fill the stack with values from 0 to 100
            for (int y = 0; y != 100; y++ )
            {
                stack.Push(y);
            }

            //print all items from stack
            foreach (var item in stack) 
            {
                Console.WriteLine(item.ToString());
            }

            Console.WriteLine("\n\nEnter an index to remove: ");
            index = Convert.ToInt32(Console.ReadLine());
            array = remover(stack, index);

            Console.WriteLine("\n\n" + array[1].Pop().ToString() + "\n\n"); //print the value of the removed index

            //print the rest of the values
            foreach(var item in array[0])
            {
                Console.WriteLine(item.ToString());
            }
        }

        public static Stack[] remover(Stack stack, int index)
        {
            Stack holding_stack = new Stack(); // used for holding values temporarily
            Stack value_stack = new Stack();   // will be returned with the desired index only
            int stack_length = stack.Count;
            int target = index - 1; // the index before the one we want to remove
            int current_index = 0;

            //if the index is larger than the stack size
            if(index > stack_length)
            {
                throw new Exception("Index bigger than stack!");
            }

            //pop items from stack and place them onto a temporary stack until we reach target
            while(current_index != target)
            {
                holding_stack.Push(stack.Pop()); //ERROR OCCURS HERE, System.InvalidOperationException, says that the stack is empty?
            }

            value_stack.Push(stack.Pop()); // push the index we were passed onto our third stack

            //place all the values from the holding stack back onto the passed stack
            while(holding_stack.Count != 0)
            {
                stack.Push(holding_stack.Pop());
            }

            return new Stack[]{stack, value_stack};

        }
    }
}

2 个答案:

答案 0 :(得分:6)

看看你的循环:

while(current_index != target)
{
    holding_stack.Push(stack.Pop());
}

您如何期待循环完成?你不能在循环体中改变targetcurrent_index ......也许你想在循环中增加current_index?如果是这样,我可以建议for循环比while循环更简单吗?

作为旁注,值得遵循.NET命名约定 - 其中方法为PascalCased,变量为camelCased,没有下划线。

所以你最终会得到:

for (int i = 0; i < target; i++)
{
    holdingStack.Push(stack.Pop());
}

答案 1 :(得分:2)

好吧,我在本节中看到一个问题:

if(index > stack_length)
{
    throw new Exception("Index bigger than stack!");
}

它应该是index >= stack_length,因为如果你的堆栈中有 100 项,并且你试图获得 100th 索引,那么你将会是超出范围,因为最后一项将是索引 99