Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 09-26-2012, 10:39 AM   PM User | #2
thesam101
New Coder

 
Join Date: Apr 2010
Location: Norfolk, England
Posts: 63
Thanks: 1
Thanked 14 Times in 14 Posts
thesam101 is an unknown quantity at this point
Hi ThetaZero

You are setting the value of i relative to the size of the ArrayList, but you are removing from the array list. I haven't studied your code, but you may want to replace this:

temp.size()

with the count variable.
__________________
//Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//
thesam101 is offline   Reply With Quote
Old 09-29-2012, 08:18 PM   PM User | #3
renter
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
renter is an unknown quantity at this point
Code:
package temp;
import java.util.ArrayList;
public class Temp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ArrayList<Integer> something = new ArrayList();
        for (int x = 0; x < 10; x++){
            something.add(x);
        }
        Temp tmp = new Temp();
        tmp.func(something);
    }
    public ArrayList func(ArrayList temp) {
        if (temp.size() > 0) {
            for (int y=0; y < temp.size(); y++) {
                System.out.print(temp.get(y) + " ");
            }
            System.out.println();
                temp.remove(0);
            func(temp);            
        }
        return temp;
    }
}


OUTPUT ::

0 1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
2 3 4 5 6 7 8 9 
3 4 5 6 7 8 9 
4 5 6 7 8 9 
5 6 7 8 9 
6 7 8 9 
7 8 9 
8 9 
9
renter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:13 PM.


Advertisement
Log in to turn off these ads.