Java Stack方法(multipop)初学者java [英] Java Stack method (multipop) Beginner java
本文介绍了Java Stack方法(multipop)初学者java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写Java方法以在堆栈上进行多播".
I'm trying to write a Java method to preform a "multi-pop" off a stack.
它应在stack
对象k
上执行弹出"操作的次数.这就是我的想法,但这并不完全正确.有帮助吗?
It should perform a "pop" operation on a stack
object k
number of times. This is what I'm thinking, but it's not quite right. Any help out there?
public void multipop(int k) {
while (top != null) {
for (int i = 0; i <= k; i++) {
this.pop();
}
}
}
推荐答案
看起来像是一个错误的错误.
Looks like an off-by-one error.
如果是k=1
,您将通过i=0
和i=1
进行循环.您可以通过将i<=k
更改为i<k
If k=1
, you will go through the loop with i=0
and i=1
. You can fix this by changing i<=k
to i<k
这篇关于Java Stack方法(multipop)初学者java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文