由LintCode问题子集出发,浅析ArrayList的拷贝问题

在做LintCode上的递归类题目子集时,我一开始的想法是递归到最后一层即单元素时然后开始逐层返回,产生相应的每层的子集并添加到最终的结果中去。于是乎有了以下代码:

public List<List<Integer>> findSolution(int[] nums, int begin, int end){
        List<List<Integer>> result = new ArrayList<>();
        List<List<Integer>> pre_result = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        temp.add(nums[end]);
        if(begin==end){
            result.add(temp);
            return result;
        }
        end--;
        pre_result = findSolution(nums,begin,end);
        result = new ArrayList<>(pre_result);
        end++;
        for(List<Integer> list:pre_result){
            list.add(nums[end]);
            Collections.sort(list);
            result.add(list);
        }
        result.add(temp);
        return result;
    }
     
    public List<List<Integer>> subsets(int[] nums) {
        // write your code here
        List<List<Integer>> result = new ArrayList<>();
        if(nums.length>0){
            result = findSolution(nums,0,nums.length-1);
        }
        List<Integer> temp = new ArrayList<>();
        result.add(temp);
        return result;
    }

算法似乎很正确,每层返回处理的时候有一个pre_result和result两个List,其中result由new ArrayList<>(pre_result)进行复制。然后对pre_result进行遍历,将其中每个List的元素加上当前层的元素后加入到result中得到结果并返回。看起来似乎毫无破绽,然后我放心的在遍历pre_result时对其中的元素进行修改,但在提交的时候出现了问题,WA。然后我进行调试处理,以[1,2,3]为样例进行输入,在遍历的时候输出result和list,以下为输出结果:

list [1, 2]
result [[1, 2], [1, 2]]
list [1, 2, 3]
result [[1, 2, 3], [1, 2, 3], [2], [1, 2, 3]]
list [1, 2, 3, 3]
result [[1, 2, 3, 3], [1, 2, 3, 3], [2], [1, 2, 3, 3], [1, 2, 3, 3]]
list [2, 3]
result [[1, 2, 3, 3], [1, 2, 3, 3], [2, 3], [1, 2, 3, 3], [1, 2, 3, 3], [2, 3]]

对结果进行分析:在改变pre_result中的元素时,如果按之前的想法走,result中的元素不可能出现修改,也就是第一个result应该是 [[1], [1, 2]],但是结果却是[[1, 2], [1, 2]]。这极有可能是因为修改pre_result中的元素的时候result中的元素也跟着被修改掉了。这就说明new ArrayList<>()的拷贝是浅拷贝类型的,它只是一个引用的拷贝而非真正的对象的拷贝。当list在修改对象中内容的时候,result的值当然会发生变化。因此我对遍历这一过程的代码进行修改,以下为重写代码:

for(List<Integer> list:pre_result){
            List<Integer> temp_list = new ArrayList<>(list);
            temp_list.add(nums[end]);
            System.out.println("list "+list);
            System.out.println("temp_list "+temp_list);
            Collections.sort(temp_list);
            result.add(temp_list);
            System.out.println("result "+result);
        }

新建一个temp_list = new ArrayList<>(list),原以为以这种方式拷贝List也只是徒劳无功,修改了temp_list后list也会跟着改变。但没想到在测试的时候AC了,这就说明了list并未随着temp_list的元素增加而增加。然后我在遍历中输出temp_list和list这两个List,以下为结果:

list [1]
temp_list [1, 2]
result [[1], [1, 2]]
list [1]
temp_list [1, 3]
result [[1], [1, 2], [2], [1, 3]]
list [1, 2]
temp_list [1, 2, 3]
result [[1], [1, 2], [2], [1, 3], [1, 2, 3]]
list [2]
temp_list [2, 3]
result [[1], [1, 2], [2], [1, 3], [1, 2, 3], [2, 3]]

果然和我的猜测一致。但是从这一测试结果看仿佛ArrayList又不是简简单单的浅拷贝了,当temp_list在添加元素的时候并不对list造成影响。为了测试当list添加元素时会不会对temp_list造成影响我又在拷贝这一语句后加上了list.add(999),结果有以下输出:

list [1, 999]
temp_list [1, 2]
result [[1, 999], [1, 2]]
list [1, 999, 999]
temp_list [1, 999, 3]
result [[1, 999, 999], [1, 2], [2], [1, 3, 999]]
list [1, 2, 999]
temp_list [1, 2, 3]
result [[1, 999, 999], [1, 2, 999], [2], [1, 3, 999], [1, 2, 3]]
list [2, 999]
temp_list [2, 3]
result [[1, 999, 999], [1, 2, 999], [2, 999], [1, 3, 999], [1, 2, 3], [2, 3]]

这就说明了list元素的添加也不对temp_list造成影响。从以上的分析就可以得出对ArrayList拷贝类型的结论了:其应当介于浅拷贝与深拷贝之间。当用List<Integer> temp_list = new ArrayList<>(list)这一方式进行拷贝时,当前在list中的元素的拷贝方式为浅拷贝,只是拷贝一个引用而已。无论是在temp_list还是list中对这些旧元素进行操作时,两者均会受到影响,这是浅拷贝的概念;而在list或temp_list中添加或删除新元素时,它们两者互不干扰,这是深拷贝的概念。

posted @ 2017-08-31 14:45  Revenent  阅读(147)  评论(0编辑  收藏  举报