![]() |
| ||
| Re: i need help on stack. If pop: stack is empty. Your code in "push" takes two arguments: An integer representing the index you're "pushing" the item onto, and the item that you want to push onto the stack. You got the Exception because you tried to push an item onto the stack at index = 3, but the array is too small; therefore, you can't push anything onto index 3 since it doesn't have 3 indexes. Why do you need to give the 'push' method an index, anyway? Instead, you could have a variable 'currentSize' in the MStack class that keeps track of how large your stack is. Then, in your push() method, you'd just automatically add the new item onto the end and increment the 'currentSize' variable. A possible problem: Since you are using an array, and not an Arraylist, you need to make sure that you allocate enough space (make the array big enough) to begin with. Otherwise, at some point, you might get an ArrayIndexOutOfBoundsException. If your array reaches it's maximum size (or at some point before it reaches its max size), you will have to make a new array which has a bigger size and copy the old one over to it. So somehow you're either not making your array big enough or you're calculating the wrong index (that you pass to the push method). |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. So you have multiple stacks, then. What I said still applies... you're either not allocating enough space or you're trying to put something in the array at the wrong index. You might want to also check that you actually allocated the space to begin with and that the array isn't null. |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
Quote:
public Node[] stack; // this is supposedly my array how do i access each stack anyway? i had those T[] (top) and B[] (base pointer) to be able to keep track of the stack but i don't know how to use them to access the stack. Or do i use them to acces the stack? I don't know if you had run my program. but if you could, or somebody else could, i'd appreciate it very much.. |
| ||
| Re: i need help on stack. If pop: stack is empty. newNode.link = top; top = newNode; These lines of code don't make sense. Whatever the current 'top' Node is, you should be assigning it's 'next node' pointer to newNode. Since you're keeping it in an array, you could just drop the whole 'linked list' thing and put your stack in the array. It seems you are mixing concepts. |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
I'm pretty sure i am clear of what i want, but can't understand how to do it.. :| thanks for your replies. here's the edited code;
import java.io.*; I can now push strings onto shelf , 2, and 3 BUT i think it is in a single stack.. :( |
| All times are GMT -4. The time now is 2:20 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC