【发布时间】:2015-03-03 07:20:14
【问题描述】:
我有一个堆栈接口和一个实现接口中方法的类(push、pop、isEmpty 和isFull)。
我无法显示堆栈的内容。这是我的主要课程。
StackInterface si = new MyStack();
System.out.println("Stack is empty: "+si.isEmpty());
si.push("Hello");
si.push("Adam");
si.push("Horrigan");
si.isEmpty();
si.pop();
si.isFull();
System.out.println(si);
输出是:
Stack is empty: true
stack.MyStack@15db9742
我在想,堆栈的内容怎么没有输出?
编辑,这里是 MyStack 类。
public class MyStack implements StackInterface {
public ArrayList<String> theStack;
public MyStack() {
theStack = new ArrayList<String>();
}
public boolean isEmpty() {
return theStack.isEmpty();
}
public boolean isFull() {
return false;
}
public void push(Object newItem) {
theStack.add((String) newItem);
}
public Object pop() {
if (!(theStack.isEmpty())) {
return theStack.remove(0);
} else {
return null;
}
}
}
【问题讨论】:
标签: java methods interface stack