开发者

Stack not returning empty even though it has no objects

开发者 https://www.devze.com 2023-04-08 09:54 出处:网络
This is seriously confusing and frustrating me. I already asked one question regarding this same program here. Going off of that code, I\'m having yet another problem with Stack. Here is a method that

This is seriously confusing and frustrating me. I already asked one question regarding this same program here. Going off of that code, I'm having yet another problem with Stack. Here is a method that is using the array of stacks, called blocks, from the previous post:

static void pileOnto(int sBlock, int rBlock)
    {
        boolean flag = true;
        while ((!blocks[rBlock].empty()) && (flag))
        {
            retainer.push((Integer)blocks[sBlock].pop());
            if (((Integer)blocks[rBlock].peek()).intValue() == sBlock) {flag = false;}
        }
        while (((Integer)blocks[rBlock].peek()).intValue() != rBlock)
        {
            returnBlock(((Integer)blocks[rBlock].pop()).intValue());
        }
    }

The first while loop should either end when the stack is empty, or when the last Integer it popped was the same as sBlock. The problem that I'm having is that blocks[rBlock].empty() never returns true, even when the program crashes from trying to pop off of blocks[rBlock], meaning that there can't be anything in the stack. 开发者_如何学CCan someone please explain to me what is going on?


In the first loop you are testing one stack (i.e. blocks[rBlock].empty()) and popping a different stack (i.e. blocks[sBlock].pop()).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号