简体   繁体   English

在Java中跟踪递归方法时遇到问题

[英]Having trouble tracing recursive method in Java

This is a rather quick question. 这是一个相当快速的问题。 I have been having trouble understanding the concept of recursion in Java and I was wondering if someone could help me. 我一直无法理解Java中递归的概念,我想知道是否有人可以帮助我。 I have been tracing successfully 5 out of the 6 recursive methods that I have done, but this one is throwing me for a loop, literally. 我已经成功地跟踪了我已经完成的6个递归方法中的5个,但是这个方法让我循环了一个循环。 I was expecting the output to be just 2(the output I traced), but when I put it into my compiler it came out with 1213121(proper output). 我期待输出只是2(我跟踪的输出),但当我把它放入我的编译器时,它出现了1213121(正确的输出)。 As I said, I have been able to do this thus far, but this one is confusing me. 正如我所说,到目前为止我已经能够做到这一点,但这个让我很困惑。 Here is what I am working with: 这是我正在使用的:

public class Recursion
{
    public static void main(String [] args)
    {
        Recursion r = new Recursion();
        r.doSomething(3);
    }

    public void doSomething(int n)
    {
        if (n > 0)
        {
            doSomething(n-1);
            System.out.print(n);
            doSomething(n-1);
        }
    }
}

I would prefer to do this in the comments, but the formatting of the comments make it hard to follow. 我更愿意在评论中这样做,但评论的格式化使得很难遵循。 You can follow this recursion as if it were a math equation. 您可以按照此递归进行操作,就好像它是一个数学方程式一样。

The basic logic is: 基本逻辑是:

DoSomething(n) results in: DoSomething(n)导致:

  • doSomething(n-1) doSomething的第(n-1)
  • print n 打印
  • doSomething(n-1) doSomething的第(n-1)

So let's follow this for doSomething(3) 所以,让我们按照这个做doSomething(3)

  • doSomething(2) doSomething的(2)
  • print 3 打印3
  • soSomething(2) soSomething(2)

So now we have to figure out what doSomething(2) does, so just plug in the values with n=2: 所以现在我们必须弄清楚doSomething(2)做了什么,所以只需插入n = 2的值:

  • doSomething(1) doSomething的(1)
  • print 2 打印2
  • doSomething(1) doSomething的(1)

Now plug in the values for n=1: 现在插入n = 1的值:

  • doSomething(0) doSomething的(0)
  • print 1 打印1
  • doSomething(0) doSomething的(0)

doSomething(0) is the base case, where the recursion stops. doSomething(0)是基本情况,递归停止。 Basically doSomething(0) does nothing. 基本上doSomething(0)什么都不做。

Therefore, the actions for n=1 become 因此,n = 1的动作变为

  • print 1 打印1

Therefore, the actions for n=2 become 因此,n = 2的动作变为

  • print 1 打印1
  • print 2 打印2
  • print 1 打印1

Therefore, the actions for n=3 become 因此,n = 3的动作变为

  • print 1 打印1
  • print 2 打印2
  • print 1 打印1
  • print 3 打印3
  • print 1 打印1
  • print 2 打印2
  • print 1 打印1

Here is the diagramatic representation of how the control flows through this recursion program. 下面是控制如何通过此递归程序的图表表示。

I think the diagram should clear your doubts. 我认为图表应该清除你的疑虑。

在此输入图像描述

Let's give line numbers to below code to ease further explanation. 让我们给下面的代码提供行号,以便进一步解释。

  1. doSomething(n-1); doSomething的第(n-1);
  2. System.out.print(n); 是System.out.print(N);
  3. doSomething(n-1); doSomething的第(n-1);

    line 1 redirect call until you reach at n=1. 第1行重定向呼叫,直到您到达n = 1。 In the end line will call doSomething(0). 在结束行中将调用doSomething(0)。 because n>0, it will return and will print 1. Then again line 3 will call doSomething(0) and will return to previous caller which was n=2. 因为n> 0,它将返回并将打印1.然后第3行将调用doSomething(0)并返回前一个调用者,即n = 2。 It will print 2 and will call doSomething(1). 它将打印2并将调用doSomething(1)。 Which will print 1 (121) and will return to n=2,line3. 这将打印1(121)并将返回n = 2,line3。 From here it will return to previous caller where n=3. 从这里它将返回到前一个调用者,其中n = 3。 Line 2 will print 3(1213). 第2行将打印3(1213)。 Then line 3 will call doSomething(2) which will append 121 as previous call which will ultimately become 1213121. 然后第3行将调用doSomething(2),它将附加121作为前一次调用,最终将变为1213121。

I hope this helps. 我希望这有帮助。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM