Hello. I'm not very familiar with recursive programming, and my problem here is that the recursive statements dont seem to be returning properly to an upper layer. While writing out the process on paper I feel this code should be correct, but it isn't working properly. The below method is initially called by an array list with a count equal to its size.
Code:
public static void powerSets(ArrayList<Integer> array, int count)
{
ArrayList<Integer> temp = array;
if (count == 0)
return;
else
{
for (int i = 0; i <= temp.size(); i++)
{
System.out.println(temp.toString());
temp.remove(i);
powerSets(temp, temp.size());
}
return;
}
The return looks similar to:
Code:
[x, y, z]
[y, z]
[z]
where it removes the first item each time (i = 0) but it never seems to step up properly and advance i to 1. The next lines are intended to be:
Code:
[y]
[x, z]
[z]
[x]
[x, y]
[y]
[x]
What am I doing wrong that is keeping recursion from bumping up a layer and incrementing the for loop properly?