English 中文(简体)
java保留在休养职能中的信息
原标题:java retain information in recursive function

有可能通过 j的助手功能保留信息,而不使用静态变量。

For example,

public void foo(){
    int v = 0;
    fooHelper(2);
}

public void fooHelper(int depth){
    v++;
    fooHelper(depth-1)
}

也就是说,我想更新变数,而不必披露每个休养案件的信息,而不必查阅职能之外的变数。

最佳回答

了解所有回答,告诉你申报属性,或更新每份复程呼吁中的 mu物体。 在一种真正的功能性的复习式中,通过将信息作为参数和/或回归类型,“获取”信息。

请允许我以一个简单的例子来说明,你希望重新计算<条码>()中各项内容的总和。 页: 1 (在补习电话之间需要保留的信息)是目前阵列中的指数和迄今为止的总和。 这里是怎样做的:

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));

如你所知,没有必要宣布(统计或案例)属性,也无需通过和修改 objects(清单、地图)——我甚至不使用当地变量,因为解决问题所需的所有信息都作为方法参数存在。

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().

问题回答

在范围上宣布的变量(例如方法)只在这一范围内(如在另一种方法中)。

如果该信息仅与这种方法有关,则该方法中的变量保持不变。 如果该信息与整个物体/类别国家有关,则保持该类别成员(静态/非静态)。

例如:

public void someRecursiveMethod(int num) {
    while (num < 10) {
        num++;
        someRecursiveMethod(num);
        System.out.println("Current num = " + num);
    }
}

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

为什么不能使其成为一个实例变数(不一定是静态的)。

public class Recursive {
    int v = 0;
    public void foo(){

        fooHelper(2);
        System.out.println(v);
    }

    public void fooHelper(int depth){
        v++;
        if(depth-1!=0)//Added this because I was getting an StackOverflowError
        fooHelper(depth-1);
    }

    public static void main(String[] args) {
        Recursive r = new Recursive();
        r.foo();
    }
}

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;
}

由于变数是原始类型,因此对变数的改动在职能范围外不会明显。 你可以宣布变数在班子内,如国家,并将国家反对变成休养职能,以取得必要的效果。

public void foo(){
    State state = new State();
    fooHelper(state, 2);
}

public void fooHelper(State state, int depth){
    state.v++;
    fooHelper(state, depth-1);
}

class State {
    int v;
}

希望会有所帮助。

你们可以通过一个物品储存每次休养电话的最新消息。 类似于下文。

public static void fooHelper(int depth, HashMap map){
      map.put(depth, "Call " + depth);
      if (depth > 0)
      {
        fooHelper(depth-1, map);
      }
      return;
    }

我认为,这被称为“民主化”。 它希望

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 ;
           }
     }
}

你们应当能够从这一算法中获得体面(而不是最佳)的业绩。 复习性纤维素算法具有可恶性能的主要原因是b/c 反复计算同样的数值。 随着再保险加血化,它从未计算过任何价值一次以上。

由于“阿里斯蒂德”指出了纪念与改观之间的微妙差异。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签