Java 在递归方法中,类级数组和数组哪个更优化

Java 在递归方法中,类级数组和数组哪个更优化,java,android,performance,recursion,arraylist,Java,Android,Performance,Recursion,Arraylist,我有两个选择 在类级别声明数组 在某处创建此数组并调用递归方法,然后在执行所需操作后返回此数组 现在考虑我正在使用一个后台线程,它从服务器加载一些数据并保存在数组中,比如 private int MAX_VALUE = 50; private int currentPage = 0; 选项1数组的示例在类级别声明 // array declaration at class level. List<SomeClass> array = new ArrayList<SomeCla

我有两个选择

  • 在类级别声明数组
  • 在某处创建此数组并调用递归方法,然后在执行所需操作后返回此数组
  • 现在考虑我正在使用一个后台线程,它从服务器加载一些数据并保存在数组中,比如

    private int MAX_VALUE = 50;
    private int currentPage = 0;
    
    选项1数组的示例在类级别声明

    // array declaration at class level.
    List<SomeClass> array = new ArrayList<SomeClass>();
    
    public void testMethod(int size) {
    
                new AsyncTask<Integer, Void, Integer>() {
    
                    @Override
                    protected Integer doInBackground(Integer... params) {
                        // some network operation which takes page number
                        // returns some data and save it to array
                        List<SomeClass> data = someNetworkOperation(params[0]);
                        array.addAll(data);
                        // size loaded data 
                        return data.size();
                    }
    
                    protected void onPostExecute(Integer result) {
                        if (result.intValue() == MAX_VALUE) {
                            // data couldn't load completely load next page
                            testMethod(++currentPage);
                        } else{
                            // date loaded use array for next operation
                            return;
                        }
                    };  
                }.execute(new Integer[] { size });
        }
    
    //类级别的数组声明。
    列表数组=新的ArrayList();
    公共void测试方法(int-size){
    新建异步任务(){
    @凌驾
    受保护的整数doInBackground(整数…参数){
    //某些网络操作占用了页码
    //返回一些数据并将其保存到数组中
    列表数据=someNetworkOperation(参数[0]);
    addAll(数据);
    //加载数据的大小
    返回data.size();
    }
    受保护的void onPostExecute(整数结果){
    if(result.intValue()=最大值){
    //数据无法完全加载到下一页
    testMethod(++currentPage);
    }否则{
    //为下一个操作使用数组加载的日期
    返回;
    }
    };  
    }.execute(新整数[]{size});
    }
    
    第二个选项使用递归的每个调用传递数组到下一个递归步骤,如

    public List<SomeClass> useTestMethod() {
            return testMethod(currentPage, new ArrayList<SomeClass>());
        }
    
        public List<SomeClass> testMethod(int size, final List<SomeClass> array) {
    
            new AsyncTask<Integer, Void, Integer>() {
    
                @Override
                protected Integer doInBackground(Integer... params) {
                    // some network operation which returns some data and save it to
                    List<SomeClass> data = someNetworkOperation(params[0]);
                    array.addAll(data);
                    return data.size();
                }
    
                protected void onPostExecute(Integer result) {
                    if (result.intValue() == MAX_VALUE) {
                        testMethod(++currentPage, array);
                    } else {
                        // data loaded pass array to do next step
                    }
                };    
            }.execute(new Integer[] { size });
            return array;
        }
    
    公共列表useTestMethod(){
    返回testMethod(currentPage,new ArrayList());
    }
    公共列表测试方法(整数大小,最终列表数组){
    新建异步任务(){
    @凌驾
    受保护的整数doInBackground(整数…参数){
    //一些网络操作,返回一些数据并保存到
    列表数据=someNetworkOperation(参数[0]);
    addAll(数据);
    返回data.size();
    }
    受保护的void onPostExecute(整数结果){
    if(result.intValue()=最大值){
    testMethod(++currentPage,数组);
    }否则{
    //加载的数据传递数组以执行下一步
    }
    };    
    }.execute(新整数[]{size});
    返回数组;
    }
    

    问题是哪种解决方案在内存分配和性能方面最有效。你的想法是什么???

    从内存分配的角度来看,没有区别,除非数组总是(!)非常小,并且可以进行堆栈分配(在方法2中)。 否则,主要因素将是递归。由于JVM目前没有实现尾部递归消除,因此第一种方法将是最有效的方法

    据我所知,你尝试的第二种选择是错误的

    原因

    假设您有一个包含2000个元素的
    数组
    ,它正在调用一个AsyncTask(意味着后台操作),在刚刚启动AsyncTask之后,它将执行下一个return语句,而AsyncTask仍在处理它。因此它不会返回完整的数组

    public List<SomeClass> testMethod(int size, final List<SomeClass> array) {
    
            new AsyncTask<Integer, Void, Integer>() {
    
                @Override
                protected Integer doInBackground(Integer... params) {
                    // some network operation which returns some data and save it to
                    List<SomeClass> data = someNetworkOperation(params[0]);
                    array.addAll(data);
                    return data.size();
                }
    
                protected void onPostExecute(Integer result) {
                    if (result.intValue() == MAX_VALUE) {
                        testMethod(++currentPage, array);
                    } else {
                        // data loaded pass array to do next step
                    }
                };    
            }.execute(new Integer[] { size });
            return array;
        }
    
    public List testMethod(整数大小,最终列表数组){
    新建异步任务(){
    @凌驾
    受保护的整数doInBackground(整数…参数){
    //一些网络操作,返回一些数据并保存到
    列表数据=someNetworkOperation(参数[0]);
    addAll(数据);
    返回data.size();
    }
    受保护的void onPostExecute(整数结果){
    if(result.intValue()=最大值){
    testMethod(++currentPage,数组);
    }否则{
    //加载的数据传递数组以执行下一步
    }
    };    
    }.execute(新整数[]{size});
    返回数组;
    }
    
    您可以分析它,而不是猜测:-)。一般说来,递归被认为是浪费的。比在类级别声明数组更有用??您是否选择在批处理和一次检索所有内容之间进行选择?或者在静态地或动态地存储(相同数量的..)数据之间?考虑到我请求服务器代表当前页面发送消息,而用户服务器不能同时发送所有消息一次,但一次支持一次寻呼请求。