0

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. Can someone please explain to me what is going on?

1
  • I am assuming you can't use generics, unboxing, removing brackets or break to make the code more readable. Commented Sep 27, 2011 at 4:37

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.