【发布时间】: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 方法中。 -
对不起我的不好^_^虽然谢谢