java retain information in recursive function

后端 未结 8 1953
滥情空心
滥情空心 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: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;
        }
    

提交回复
热议问题