我正在做考试题目,遇到了这个问题。我不明白这个问题的意思。
问题:当D为空时,method1(-1)的结果是什么?
我知道当 D 为空(或 = 0)时,该方法已达到基本情况,递归方法结束。我不确定 method(-1) 是什么意思。
如果有人能为我解释一下,我将不胜感激。谢谢!
public void method1(int n){
if (n == 0) return;
else{
String str = (String) D.dequeue();
D.enqueue(str);
D.enqueue(str);
method1(n-1);
}
}
public void method2(){
method1(D.size())
}
最佳答案
该方法将永远运行(或直到由于内存不足问题而失败),因为递归调用为 n-1,因此永远不会达到 n=0 时的最终情况。
当列表为空时,D 类可能会抛出异常...
关于java - 递归队列方法1(-1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899432/