java retain information in recursive function

后端 未结 8 1886
滥情空心
滥情空心 2020-12-29 09:05

Is it possible to retain information via a helper function with java, without using static variables.

For example,

public void foo(){
    int v = 0;
         


        
相关标签:
8条回答
  • 2020-12-29 09:28

    Forget about all the answers that tell you to declare attributes, or to update mutable objects in each recursive call. In a true functional, recursive style you "retain" information by passing it as parameters and/or return types.

    Let me illustrate with a simple example, let's say that you want to recursively calculate the sum of the elements in an int[]. Here, the state (the information that needs to be retained between recursive calls) is the current index in the array and the sum so far. Here's how to do it:

    public int sum(int[] array) {
        return sum(array, 0, 0); 
    }
    
    private int sum(int[] array, int idx, int acc) {
        if (idx == array.length)
            return acc;
        return sum(array, idx+1, acc+array[idx]);
    }
    

    Call it like this:

    int[] array = {1, 2, 3};
    System.out.println(sum(array));
    

    As you can see, there's no need to declare (static or instance) attributes, and no need to pass and modify mutable objects (lists, maps) - I'm not even using local variables, because all the required information needed to solve the problem is present as method parameters.

    In the code in your question the v variable is supposed to do what the acc parameter is doing in my answer, namely: modifying an accumulated value each time the recursion is called. In the end, you just need to return the accumulated value from the helper function (which must not have a void return type) and that's how you'll get the value in foo().

    0 讨论(0)
  • 2020-12-29 09:31

    You can create a new class (yuck), or pass the variable as a parameter and return it in fooHelper.

    0 讨论(0)
  • 2020-12-29 09:37

    A variable declared in a scope (for example method) is accessible only in this scope (e.g. not in another method).

    If the information is relevant for the method only, keep the variable in the method. If the information is relevant for the whole object / class state, keep it a class member (static/non static).

    For example:

    public void someRecursiveMethod(int num) {
        while (num < 10) {
            num++;
            someRecursiveMethod(num);
            System.out.println("Current num = " + num);
        }
    }
    
    0 讨论(0)
  • 2020-12-29 09:40

    You could return a list or a similar data structure:

    public List<Integer> fooHelper( int v, int depth ){
        if( depth == 0 ) return new ArrayList();
    
        v++;
        List<Integer> result = fooHelper( v, depth-1 );
    
        result.add( new Integer(v) );
    
        return result;
    }
    
    0 讨论(0)
  • 2020-12-29 09:40

    I think this is called memoization. It looks like

    class Fibonacci
    {
         public Map < Integer , Integer > memorized = new HashMap < > ( ) ;
    
         public int fib ( int n )
         {
               if ( memoized . containsKey ( n ) )
               {
                     return memoized . get ( n ) ;
               }
               else
               {
                     int fib = // calculate recursively
                     memoized . put ( n , fib ) ;
                     return fib ;
               }
         }
    }
    

    You should be able to get decent (not optimal) performance out of this algorithm. The primary reason that the recursive fibonacci algorithm has horrible performance is b/c it is repeatedly calculating the same values. With recursion+memoization it never calculates any value more than once.

    Thanks to @Aristide for pointing out the subtle difference between memorization and memoization.

    0 讨论(0)
  • 2020-12-29 09:43

    You can pass an object to store the update for each recursive call. Something like the one below.

    public static void fooHelper(int depth, HashMap map){
          map.put(depth, "Call " + depth);
          if (depth > 0)
          {
            fooHelper(depth-1, map);
          }
          return;
        }
    
    0 讨论(0)
提交回复
热议问题