简体   繁体   English

在方法本身调用后有return语句的情况下,以Java递归

[英]Recursive in Java when there is a return statement after the method called itself

Code: 码:

public static void main(String[] args) {
    System.out.println(test(13549));
}    

public static int test(int a){
    if(a<10)
        return a;
    int b = (a%10);
    int c = test(a/10);
    int d = Math.max(b,c);
    return d;
}

I understand what the method does (after using the debugger) and I understand that the method calls itself till it is less than 10 and it runs and checks what's bigger, b or c. 我了解该方法的作用(在使用调试器之后),并且我了解该方法会自我调用,直到小于10,然后运行并检查更大的是b还是c。 Now what I don't understand is that why when there is the return statement return d; 现在我不明白的是为什么当有return语句return d; it returns to int c = test(a/10) and not to the start of the method of int test(int a){ . 它返回int c = test(a/10)而不是int test(int a){的方法的开始。

The return statement returns the output of the call to test . return语句返回test的调用输出 So on the line return d; 这样就return d; it's just returning the value of test(a/10) in the line c = test(a/10) . 它只是返回的值test(a/10)在线路c = test(a/10)

This is a basic idea of recursion - when you call the method within itself, it isn't just jumping back to the start of the method. 这是递归的基本思想-当您在内部调用方法时,它不只是跳回到方法的开始。

visually: 视觉:

1
|
|  2
|__|
|  |  3
|  |__|
   |  |  x
   |  |__|
      |  .
      |  .

Where each vertical line the running of the function, and the horizontal lines are recursive calls. 其中每条垂直线是函数运行的地方,而水平线是递归调用。

After each call, it returns back to the place in the instance of the method it was called in Effectively, that means that in the 3rd recursion, you have 3 versions of the function on the stack (in memory), rather than simply going back to the top of the current function 3 times. 每次调用之后,它返回到在有效地被调用的方法实例中的位置,这意味着在第三次递归中,您在堆栈上(内存中)拥有函数的3个版本,而不是简单地返回到当前功能顶部的3倍。

When tracing your code, imagine you have a stack (of function execution). 跟踪代码时,请想象您有一个堆栈(函数执行)。 Whenever you find the function is calling itself, put a new function execution on top of the stack, and when the function returns take out the stack element. 每当您发现函数正在调用自身时,就将新的函数执行放在栈顶,并在函数返回时取出栈元素。

You'll find that the function will keep calling itself until it reach the 'base case' -- when a<10. 您会发现该函数将继续调用自身,直到达到“基本情况”为止-当a <10时。

Putting a code below a return statement won't do any good (in some language it won't even compile), none of the code will get executed once the function returns 将代码放在return语句下面不会有任何好处(在某些语言中甚至无法编译),一旦函数返回,则不会执行任何代码

Each time you call your recursive function, it starts from the top of the function. 每次调用递归函数时,它都从函数的顶部开始。 Since you have d defined after your recursive function call you will never get to d. 由于您在递归函数调用之后定义了d,所以您将永远不会到达d。 You will continue to recursively call your function until you are able to return a. 您将继续递归调用函数,直到能够返回a为止。

When you call test(13549) this is what happens: 当您调用test(13549) ,会发生以下情况:

test(13549){
   false
   b=9
   c= test(1354){
       false
       b=4
       c=test(135){
           false
           b=5
           c=test(13){
               false
               b=3
               c=test(1){
                  true
                  return 1
                }
                d=Max(3,1)
                return 3
              }
               d=Max(3,3)
               return 3
             }
             d=Max(5,3)
             return 5
           }
          d=Max(4,5)
          return 5
        }
    d=Max(9,5)
    return 9
  }

Sorry if I may have miscalculated in any way.. but there you can see, that the first return statement is not reached until all recursive calls are finished and the very first c hast an actual value. 抱歉,如果我可能以任何方式计算错误。.但是您可以看到,直到所有递归调用都完成并且第一个c具有实际值时,才到达第一个return语句。

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

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