为什么在下面的代码中ArrayList为空?

huangapple go评论68阅读模式
英文:

Why the ArrayList comes out to be empty in the below code

问题

以下是您要翻译的代码部分:

class Solution {
    public void sub(int idx, int[] nums, List<Integer> arr, ArrayList<List<Integer>> lt) {
        if (idx >= nums.length) {
            lt.add(arr);
            // System.out.println(arr.toString());
            return;
        }
        arr.add(nums[idx]);
        sub(idx + 1, nums, arr, lt);
        arr.remove(arr.size() - 1);
        sub(idx + 1, nums, arr, lt);
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> arr = new ArrayList<>();
        ArrayList<List<Integer>> lt = new ArrayList<List<Integer>>();
        sub(0, nums, arr, lt);
        return lt;
    }
}
英文:

The questions wants us to return all the subsets of an array in a list.But in the below code why The list lt remains empty after the code is executed?
According to me this list lt should have stored all the subsets....

class Solution {
    public void sub(int idx,int[] nums,List&lt;Integer&gt; arr,ArrayList&lt;List&lt;Integer&gt;&gt; lt){
        if(idx&gt;=nums.length){
            lt.add(arr);
            // System.out.println(arr.toString());
            return;
        }
        arr.add(nums[idx]);
        sub(idx+1,nums,arr,lt);
        arr.remove(arr.size()-1);
        sub(idx+1,nums,arr,lt);
    }

    public List&lt;List&lt;Integer&gt;&gt; subsets(int[] nums) {
        List&lt;Integer&gt; arr=new ArrayList&lt;&gt;();
        ArrayList&lt;List&lt;Integer&gt;&gt; lt=new ArrayList&lt;List&lt;Integer&gt;&gt;();
        sub(0,nums,arr,lt);
        return lt;
    }
}

答案1

得分: 1

The list lt remains empty after the code is executed?

这个列表 lt 在代码执行后仍然保持为空吗?

The List of Lists doesn't. However, every time you add a List to that collection, it is the same List object. You ultimately remove every element you ever add to that List, so you end up with a List of many references to the same empty List.

List 中的 List 不会保持为空。然而,每次将一个 List 添加到该集合中,它实际上都是同一个 List 对象。最终,你会移除你添加到该 List 中的每个元素,因此最终会得到一个包含对同一个空 List 的多个引用的 List

Instead of adding an element to the one List and later removing it, make a new copy of the List each time you to add an element, and add the element (only) to the copy. Never remove any elements. This will ultimately result in 2<sup>n</sup> distinct List objects for an array of length n, which is indeed the number to be expected.

不要将元素添加到一个 List 中,然后再移除它,而是每次添加一个元素时都制作一个 拷贝List,并将元素(仅)添加到拷贝中。永远不要移除任何元素。这最终会导致对于长度为 n 的数组,会产生 2n 个不同的 List 对象,这确实是预期的数量。

英文:

> The list lt remains empty after the code is executed?

The List of Lists doesn't. However, every time you add a List to that collection, it is the same List object. You ultimately remove every element you ever add to that List, so you end up with a List of many references to the same empty List.

Instead of adding an element to the one List and later removing it, make a new copy of the List each time you to add an element, and add the element (only) to the copy. Never remove any elements. This will ultimately result in 2<sup>n</sup> distinct List objects for an array of length n, which is indeed the number to be expected.

答案2

得分: 0

以下是翻译的内容:

问题出在代码中,它在整个递归过程中修改了相同的 arr 列表实例。当它将列表 arr 添加到列表 lt 时,实际上是添加了对相同列表实例的引用。因此,当代码修改 arr 时,也同时修改了存储在 lt 中的列表。

class Solution {
    public void sub(int idx, int[] nums, List<Integer> arr, ArrayList<List<Integer>> lt) {
        if (idx >= nums.length) {
            lt.add(new ArrayList<>(arr));
            // System.out.println(arr.toString());
            return;
        }
        arr.add(nums[idx]);
        sub(idx + 1, nums, arr, lt);
        arr.remove(arr.size() - 1);
        sub(idx + 1, nums, arr, lt);
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> arr = new ArrayList<>();
        ArrayList<List<Integer>> lt = new ArrayList<List<Integer>>();
        sub(0, nums, arr, lt);
        return lt;
    }
}
英文:

The problem with the code is that it's modifying the same arr list instance throughout the recursion. When it adds the list arr to the list lt, it's actually adding a reference to the same list instance. As a result, when the code modifies arr, it's also modifying the lists stored in lt.

class Solution {
    public void sub(int idx,int[] nums,List&lt;Integer&gt; arr,ArrayList&lt;List&lt;Integer&gt;&gt; lt){
        if(idx&gt;=nums.length){
            lt.add(new ArrayList&lt;&gt;(arr));
            // System.out.println(arr.toString());
            return;
        }
        arr.add(nums[idx]);
        sub(idx+1,nums,arr,lt);
        arr.remove(arr.size()-1);
        sub(idx+1,nums,arr,lt);
    }

    public List&lt;List&lt;Integer&gt;&gt; subsets(int[] nums) {
        List&lt;Integer&gt; arr=new ArrayList&lt;&gt;();
        ArrayList&lt;List&lt;Integer&gt;&gt; lt=new ArrayList&lt;List&lt;Integer&gt;&gt;();
        sub(0,nums,arr,lt);
        return lt;
    }
}

答案3

得分: 0

这是因为你只有一个 arr 引用,而你不断地将相同的内容添加到 lt 中。最后,它成为了一个空数组,并反映在所有地方。

你可以复制 arr(克隆它),然后再添加

lt.add((ArrayList)((ArrayList)arr).clone());
英文:

This is because you have only one arr reference and you keep adding same to the lt. at the end it is blank array and reflecting to all.

You can make a copy of arr(Clone it) and then add it

lt.add((ArrayList)((ArrayList)arr).clone());

huangapple
  • 本文由 发表于 2023年4月7日 02:18:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定