简体   繁体   English

在 ArrayList 中添加数组的所有子序列 - 打印空列表

[英]Add all subsequences of an array in an ArrayList - Prints Empty List

I am trying to add all subsequences of an array in an ArrayList.我正在尝试在 ArrayList 中添加数组的所有子序列。 Below is my program where i am using recursion:下面是我使用递归的程序:

public class Subsequences {

    public static void main(String[] args) {
        int arr[] = {1,2,3};
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        printSubsequences(0,arr,list,result);
        System.out.println(result);
    }

    private static List<List<Integer>> printSubsequences(int i, int[] arr, List<Integer> list, List<List<Integer>> result) {
        if(i>=arr.length){
            result.add(list);
            return result;
        }
        list.add(arr[i]); //take
        printSubsequences(i+1,arr,list,result);
        list.remove(list.size()-1); //not take
        printSubsequences(i+1,arr,list,result);
        return result;
    }

}

The output of the following program is:以下程序的output为:

[[], [], [], [], [], [], [], []]

Why is my arraylist empty here?为什么我的 arraylist 这里是空的? Can someone please explain?有人可以解释一下吗?

You are modifing the same one inner list over and over again instead of adding new lists.您一遍又一遍地修改同一个内部列表,而不是添加新列表。 Change改变

printSubsequences(i+1,arr,list,result);

to

printSubsequences(i+1,arr,new ArrayList<>(list),result);

to create a new list and copy the current elements to a new sublist创建一个新列表并将当前元素复制到一个新的子列表

private static List<List<Integer>> printSubsequences(int i, int[] arr, List<Integer> list, List<List<Integer>> result) {
    if(i>=arr.length){
        result.add(list);
        return result;
    }
    list.add(arr[i]); //take
    printSubsequences(i+1,arr,new ArrayList<>(list),result);
    list.remove(list.size()-1); //not take
    printSubsequences(i+1,arr,new ArrayList<>(list),result);
    return result;
}

When you just use a println statement, you are just printing the current state of your list for the current recursive step.当您只使用 println 语句时,您只是为当前递归步骤打印列表的当前 state。 You are adding and removing elements to and from the same list.您正在向同一个列表中添加和删除元素。 Your list of lists contains 8 times the same list, so modifing one of them will reflect the change to each of them.您的列表列表包含 8 次相同的列表,因此修改其中一个将反映对每个列表的更改。 To give you a simple example:给你一个简单的例子:

List<List<String>> result = new ArrayList<>();
List<String>    sagarList = new ArrayList<>();
sagarList.add("Sagar");
sagarList.add("Balyan");

System.out.println("sagarList: " + sagarList);

//now let's add the same list multiple times to the result list
result.add(sagarList);
result.add(sagarList);
result.add(sagarList);
result.add(sagarList);

System.out.println("result before removing one element from sagarList: " );
System.out.println(result);

//lets make one change to the list
sagarList.remove(sagarList.size()-1);

System.out.println("result after removing one element from sagarList: ");

System.out.println(result);

output: output:

sagarList: [Sagar, Balyan]
result before removing one element from sagarList: 
[[Sagar, Balyan], [Sagar, Balyan], [Sagar, Balyan], [Sagar, Balyan]]
result after removing one element from sagarList: 
[[Sagar], [Sagar], [Sagar], [Sagar]]

If you look at your result list at the last step of your recursion the list is empty, that describes why all of your sublists were empty befor making the change:如果您在递归的最后一步查看结果列表,则列表为空,这描述了为什么在进行更改之前所有子列表都是空的:

[[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []]

because " list.remove(list.size()-1); " will clear the list finally,and the eight lists in the result are the same list因为“list.remove(list.size()-1);”最后会清空列表,结果中的8个列表是同一个列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量添加到数组列表,然后汇总 arraylist 的所有元素? - how to add variable to array list and then total all the elements of arraylist? 数组列表为所有元素打印相同的数据 - Array list prints same data for all elements 使用Java的数组的所有可能子序列 - All possible Subsequences of an array using java 在JAVA中使用递归打印数组的所有子序列 - printing all subsequences of an array using recursion in JAVA Java - 新的ArrayList(List)vs empty ArrayList()+ add(element) - Java - new ArrayList(List) vs empty ArrayList() + add(element) 为什么不能两次调用 scanEverything 方法? 它只打印一次 arrayList,第二个 println 显示一个空列表 - Why is it not possible to call the scanEverything method twice? It only prints out the arrayList once, and the second println shows an empty list Java中的ArrayList仅打印列表中的最后一个对象 - ArrayList in Java only prints the last object in the List Java HashMap <String, ArrayList<Object[]> &gt;如何添加一个空数组? - Java HashMap<String, ArrayList<Object[]>> how to add an empty array? 在给定整数数组的情况下查找所有增长最长的子序列 - 动态编程 - To find all longest increasing subsequences given an array of integers - Dynamic Programming 返回类似数组的所有子序列,仅使用Java中的连续值 - Return all subsequences of an array-like with only consecutive values in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM