【发布时间】:2013-11-07 23:42:24
【问题描述】:
我正在尝试删除堆栈中的特定元素,但遇到了一些问题。我的想法是将元素弹出到临时堆栈,弹出我正在寻找的索引,然后将临时堆栈中的元素弹出回主堆栈。我很难想出如何让临时堆栈重新回到顶部。任何帮助将不胜感激。
public E remove(int index) {
Stack<E> tmpStack = new Stack<E>();
if (size() == 0) {
return null;
} else {
for (int i = 0; i < index; i++) {
tmpStack.push(this.pop());
}
return tmpStack.pop();
}
while (!tmpStack.isEmpty())
this.push(tmpStack.pop());
}
想法?干杯!
【问题讨论】: