[英]What happens when recursion is called twice in a method?
I'm pretty new to programming and I have now come to the concept of recursion. 我对编程还很陌生,现在我有了递归的概念。 I have solved a few basic assignments but when I come to multible recursion I get terribly lost.
我已经解决了一些基本的任务,但是当我进行多重递归时,我会迷路。 I have tried to solve the following recursion several times but just cant get it right.
我试图几次解决以下递归问题,但无法正确解决。
A recursive method is called with the arguments "ABCD"
and 2
, ie recMethod("ABCD", 2);
使用参数
"ABCD"
和2
调用递归方法,即recMethod("ABCD", 2);
. 。
public void recMethod( String str, int n ) {
if( n >= 0 ) {
recMethod(str, n – 1);
System.out.print(str.charAt(n));
recMethod(str, n – 1);
}
}
Is there somebody out there who can explain what is going on? 那里有人可以解释发生了什么吗? The first recursive call really confuses me.
第一次递归调用确实使我感到困惑。
The most important thing to know about recursion is that it's nothing special . 关于递归最重要的事情是,它没有什么特别的 。 As with everything in a method it needs to finish a statement before the next statement can be executed:
与方法中的所有内容一样,它需要先完成一条语句,然后才能执行下一条语句:
public void someMethod() {
someOtherMethod();
someLastMethod();
}
Looking at my example it's obvious that someLastMethod
will be called after someOtherMethod
has finished. 看我的示例,很明显,
someOtherMethod
完成后将调用someLastMethod
。 It really doesn't matter if you replace someOtherMethod
with something recursive. 如果您用递归的方式替换
someOtherMethod
,那真的没关系。 It needs to finish before someLastMethod
can be called. 它需要先完成,然后才能调用
someLastMethod
。 When looking at your recursive method again: 再次查看递归方法时:
public void recMethod( String str, int n ) {
if( n >= 0 ) {
recMethod( str, n – 1 );
System.out.print( str.charAt( n ) );
recMethod( str, n – 1 );
} else { // base case added for clarity
return;
}
}
For each call where n >= 0
, before the System.out.print
method is called the call to recMethod
has to be called. 对于每个
n >= 0
的调用,在调用System.out.print
方法之前,必须先调用recMethod
。 Every call to recMethod has it's own n
and str
so they can be considered different methods entirely except their code is 'very similar'. 每个对recMethod的调用都有其自己的
n
和str
因此可以将它们完全视为不同的方法,只是它们的代码“非常相似”。
Every call will either match base case or need the result of the same method with n decremented so I like to start from the base case and work myself backwards, which is when n is -1
. 每个调用要么匹配基本情况,要么需要n递减的相同方法的结果,所以我想从基本情况开始并向后工作,即n为
-1
。 Imagine you call recMethod("ABCD",-1)
what would happen? 假设您调用
recMethod("ABCD",-1)
会发生什么? Well it prints nothing or "". 好吧,它什么也不打印或“”。
I then look at recMethod("ABCD",0)
and it calls the base case, which we know does nothing, then prints "A" and then it calls the same as the first statement which again does nothing. 然后,我看一下
recMethod("ABCD",0)
,它调用基本情况(我们知道什么都不做),然后输出“ A”,然后调用与第一个语句相同的内容,该语句再次不执行任何操作。 Thus it prints "A" 因此它打印“ A”
If we look at recMethod("ABCD",1)
. 如果我们看一下
recMethod("ABCD",1)
。 We know it calls recMethod("ABCD",0)
which prints "A", then it prints "B", then call recMethod("ABCD",0)
which prints "A". 我们知道它先调用
recMethod("ABCD",0)
,然后打印“ A”,然后再打印“ B”,然后调用recMethod("ABCD",0)
,它打印“ A”。 Thus it prints "ABA" 因此它打印“ ABA”
If we look at recMethod("ABCD",2)
. 如果我们看一下
recMethod("ABCD",2)
。 We know it calls recMethod("ABCD",1)
which prints "ABA", then it prints "C", then call recMethod("ABCD",1)
which prints "ABA". 我们知道它调用了
recMethod("ABCD",1)
,它打印“ ABA”,然后打印“ C”,然后调用recMethod("ABCD",1)
,它打印“ ABA”。 Thus it prints "ABACABA" 因此它打印“ ABACABA”
If we look at recMethod("ABCD",3)
. 如果我们看一下
recMethod("ABCD",3)
。 We know it calls recMethod("ABCD",2)
which prints "ABACABA", then it prints "D", then call recMethod("ABCD",2)
which prints "ABACABA". 我们知道它调用了
recMethod("ABCD",2)
,它打印“ ABACABA”,然后它打印“ D”,然后调用recMethod("ABCD",2)
,它打印“ ABACABA”。 Thus it prints "ABACABADABACABA" 因此它打印“ ABACABADABACABA”
Since "abcd".charAt(4)
won't work it doesn't make sense to go on. 由于
"abcd".charAt(4)
无法正常工作,因此没有意义继续下去。 Perhaps the code should have a test for this or perhaps it should be private and have a public one without n
that guarantees n
never goes beyond str
bounds? 也许代码应该对此有一个测试或也许它应该是私人和有没有公众一个
n
,保证n
从来没有超越str
的边界?
If you were to make a method that works recursively you do the same. 如果要创建一种递归工作的方法,请执行相同的操作。
What should happen for the base case. 基本情况会发生什么。 (How should it stop)
(应该如何停止)
What should happen if it's not the base case expressed as if the method works as intended. 如果不是将基本情况表示为该方法按预期工作,该怎么办。 The catch here is that you need to make sure that each call to the same method here is a slightly simpler problem which the recursion is bound to hit a base case or else you get infinite recursion!
这里要注意的是,您需要确保对同一方法的每次调用都是一个稍微简单一些的问题,递归必然会碰到基本情况,否则您将获得无限递归!
Thats it! 而已! It will work.
它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.