View Single Post
Old 09-25-2012, 11:35 PM   PM User | #1
ThetaZero
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ThetaZero is an unknown quantity at this point
Recursion problems

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?
ThetaZero is offline   Reply With Quote