导入java.util。*;
公共类MyStack { 私有ArrayList列表= new ArrayList <>();
public boolean isEmpty(){
return list.isEmpty();
}
public int getSize(){
return list.size();
}
public Object peek(){
return list.get(getSize()-1);
}
public Object pop(){
Object o = list.get(getSize() -1 );
list.remove(getSize() -1);
return o;
}
public void push(Object o ){
list.add(o);
}
@Override
public String toString() {
return "stack: " + list.toString();
}
public static void main(String[] args){
MyStack mystack = new MyStack();
mystack.push(mystack);
System.out.println(mystack.isEmpty());
System.out.println( mystack.getSize());
System.out.println( mystack.peek());
System.out.println(mystack.pop());
System.out.println(mystack.toString());
}
}
问题是,当我运行这段代码时,由于toString()方法,我得到了很多异常,而我不知道您能解决什么问题?