【发布时间】:2018-05-02 04:35:30
【问题描述】:
我有这样的代码:
public static void main(String[] args) throws Exception {
String popElement ;
Stack<String> stack = new Stack();
stack.push( "new" );
stack.push( "new" );
stack.push( "new1" );
stack.push( "new2" );
if(! stack.empty() )
{
do
{
popElement = stack.pop();
System.out.println( "pop element "+popElement );
}while (popElement.equals( "new" ));
}
System.out.println( "STACK :"+stack );
我想弹出堆栈直到新堆栈,我需要堆栈输出为[new,new].. 但我的代码仅弹出new2,它显示输出为[new,new,new1。我只需要[new,new]]。
【问题讨论】: