简体   繁体   English

在方法中两次调用递归时会发生什么?

[英]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的调用都有其自己的nstr因此可以将它们完全视为不同的方法,只是它们的代码“非常相似”。

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. 如果要创建一种递归工作的方法,请执行相同的操作。

  1. What should happen for the base case. 基本情况会发生什么。 (How should it stop) (应该如何停止)

  2. 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. 它会工作。

The best way to understand recursion as for me is to write it on the paper line by line. 对我来说,理解递归的最好方法是将它逐行写在纸上。 What I did for your case now. 我现在为您做的事情。 在此处输入图片说明

Please try to do the same, and don't hesitate to ask more question, I hope it helps! 请尝试做同样的事情,不要犹豫,问更多问题,希望对您有所帮助!

暂无
暂无

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

相关问题 当同一个被调用两次时,CompletableFuture 的“SupplyAsync”部分会发生什么 - What happens to the “SupplyAsync” portion of CompletableFuture when same one is called twice 在对象的方法内部调用wait方法时会发生什么? - What happens when the wait method is called inside the method of an object? 当一个线程通过另一个类调用synchronized方法时会发生什么? - What happens when a synchronized method is called by a thread via another class? 当方法调用自身的return语句会发生什么? 递归。 - What happens when a return statement where method calls itself? Recursion. 在Java中调用方法后会发生什么 - What happens after a method is called in Java 当我自己用参数字符串抛出异常时究竟会发生什么? 什么时候调用 toString 方法? - What exactly happens when I throw an exception myself with a parameter string? When does the toString method get called? 调用new ClassName()时会发生什么? - What happens when new ClassName() is called..? 当我包装两次I / O流时会发生什么? - What happens when I wrap I/O streams twice? 当以下情况发生时-A.从可运行的B中调用Activity方法。多个线程同时调用Activity方法 - What happens when- A. Activity method is called from Runnable B. Multiple threads call Activity method simultaneously 一旦调用setException方法,对Settable Future对象会发生什么? - What happens to a Settable Future object once the setException method is called on it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM