Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Recursion problems

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 09-26-2012, 12:35 AM PM User | #1 ThetaZero New to the CF scene   Join Date: Sep 2012 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts 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 array, int count) { ArrayList 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?
 09-26-2012, 11: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 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.//
 09-29-2012, 09: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 Code: ```package temp; import java.util.ArrayList; public class Temp { /** * @param args the command line arguments */ public static void main(String[] args) { ArrayList 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```

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:15 PM.