java retain information in recursive function

后端 未结 8 1954
滥情空心
滥情空心 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:40

    You could return a list or a similar data structure:

    public List fooHelper( int v, int depth ){
        if( depth == 0 ) return new ArrayList();
    
        v++;
        List result = fooHelper( v, depth-1 );
    
        result.add( new Integer(v) );
    
        return result;
    }
    

提交回复
热议问题