Reputation: 553
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?
Upvotes: 0
Views: 144
Reputation: 719596
In the first loop you are testing one stack (i.e. blocks[rBlock].empty()
) and popping a different stack (i.e. blocks[sBlock].pop()
).
Upvotes: 2