【问题标题】:how to check when stack is empty如何检查堆栈何时为空
【发布时间】:2015-07-20 13:15:29
【问题描述】:

这是我的数组堆栈,与我的主堆栈分开 我只有一个问题,我的代码没有问题,但它缺少 像如果我运行 pop 但堆栈为空,它必须有一个对话框说它是空的,我尝试了一个 if else 语句但我不知道将它放在哪里,或者它真的需要 if else 语句,无论如何这是我的代码。 . .

public class ArrayStack {
        int STACK_MAX = 20;
        int size = -1 ;
        int top = -1 ;
        int StackObj[] = new int[STACK_MAX];

          /**************** for PUSH METHOD *********/

        public void Push(int obj) {
            if (size()==STACK_MAX){
                System.out.println("STACK is FULL");
                }
            else{
                    StackObj[++top]= obj;
                }
        } 

        /**************** for SIZE Method ********/

        public int size() {
            return (top+1);    
        }

      /******************** for Display Method****/

        public void DisplayStack() {
                String disp="";

                for (int i=top; i>=0; i--){
                    disp += StackObj[i] + "\n";                     
                }
              JOptionPane.showMessageDialog(null, "Elements of the Stacks : \n" + disp);
            }

      /***************** for isEmpty Method *******/

      public boolean isEmpty(){

        return (top == -1);

      }

      /***************** for Top Method ***********/
        public int Topmethod(){

        int taas = StackObj[top];

        JOptionPane.showMessageDialog(null,"Top is : "+taas);
        return (top);
        }

      /***************** for Pop Method ***********/       
           public int pop(){

            int topItem = StackObj[top];
               top--;
            JOptionPane.showMessageDialog(null,"The recently pushed number was Deleted: "+topItem);
          return(top);
        } 
    }

【问题讨论】:

  • 您能否edit 您的标题使其更符合您的实际问题?在编辑器中还有{} 选项,可以让您正确发布代码示例。
  • 以小写字母输入的单词很难阅读,就像试图听别人喃喃自语一样。请在句子的开头使用大写字母来表示单词 I,以及像 ArrayList 或 Oracle 这样的专有名称。
  • if ( isEmpty() ) { JOptionPane.showMessage(); } 把它扔到 pop 方法中。
  • 对不起我的不好^_^虽然谢谢

标签: java stack


【解决方案1】:

您可能希望在您的 pop 方法中添加一些东西来处理识别堆栈何时为空,然后处理空堆栈的情况,例如:

public int pop(){
    if(size() == 0){  //detect empty stack
        JFrame frame = new JFrame("my frame");; //handle empty stack
        JOptionPane.showMessageDialog(frame,"Stack is empty!");
        return null; //make sure you handle a null return value if you use this
    }
    int topItem = StackObj[top];
       top--;
    JOptionPane.showMessageDialog(null,"The recently pushed number was Deleted: "+topItem);
  return(top);


} 

【讨论】:

  • 将您的 system.out 更改为 JOptionPane.showMessageDialog,您将得到他正在寻找的 imo。
  • 哦抱歉没看到他要求弹出对话,谢谢-现在编辑
  • 感谢佩吉,虽然我没有使用框架,但我改变了它。 . .它是这样的
【解决方案2】:

我在这里编辑了它

public int pop(){
    if(size() == 0){  //detect empty stack
    JOptionPane.showMessageDialog(null,"Stack is empty!");
    return (top); //make sure you handle a null return value if you use this
}
    int topItem = StackObj[top];
       top--;
    JOptionPane.showMessageDialog(null,"The recently pushed number was Deleted: "+topItem);
  return(top);


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2023-03-03
    • 2011-09-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    相关资源
    最近更新 更多