Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Stacks are FiLo, First in, last out. Generally stacks are used in a manner where you push items (the first being the bottom of the stack) and they keep on stacking. When you need to get values off of the stack you pop, going back in reverse order.</p> <p>So the order you entered for data would store as:</p> <p>1,2,3 bottom -> top</p> <p>Your data is correct, it is as it should be.</p> <p>So next you'll want to write a pop function, where you grab the value at the top of the stack and then decrement the top variable.</p> <p>Popping your stack one at a time would return in the following order:<br> 3<br> 2<br> 1<br></p> <p>From what you were describing, I think perhaps you were going to pop from the front of the stack? If that is what you want to do, you would have to shift all of your values every-time you pushed a new value. What you currently have now is better code than shifting all your values on every push =]</p> <p>You are on the right track.</p> <p><strong>EDIT</strong> I have not used swing in a while, but hopefully this will lead you in the right direction (I looked at some old code to make sure my syntax was correct, have not tested though). To answer the question you asked in my comment, your code would look something similar to the following for the pop function and setting the text for the popTextField</p> <p>your pop function should look something like the following:</p> <pre><code>/** * The code for the button press could be as simple as follows */ popBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ try{ popTextField.setText(popStack); }catch(IndexOutOfBoundsException e){ popTextField.setText("The stack is empty"); } } }); </code></pre> <p>the pop function can be really simple. You do not need to use exceptions - but it is good practice.</p> <pre><code>/** * Simple Pop function */ private int popStack(){ if(top &gt;= 0){ return myStack[top--]; }else{ throw new IndexOutOfBoundsException("The stack is empty"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload