[英]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.