在递归方法中哪一个是更优化的类级数组与数组

时间:2015-05-16 12:06:57

标签: java android performance recursion arraylist

我有两个选择。

  1. 在班级声明数组。
  2. 在某处创建此数组并调用递归方法,并在必需的操作后返回此数组。
  3. 现在考虑我正在使用后台线程从服务器加载一些数据并保存在数组中,如

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

    第二个选项使用递归的每个调用传递数组到下一个递归步骤,如

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

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

2 个答案:

答案 0 :(得分:0)

从内存分配的角度来看,没有区别,除非数组总是(!)非常小并且可以进行堆栈分配(在方法2中)。

否则,主导因素将是递归。由于JVM目前没有实现尾递归消除,第一种方法将是最有效的方法。

答案 1 :(得分:0)

  

根据我的知识你尝试的第二个选项是错误的。

<强>原因

假设您有一个array of 2000 elements,它正在调用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;
    }