Results 1 to 4 of 4
Thread: Dynamic for loop
 02072010, 07:07 PM #1Member
 Join Date
 Feb 2010
 Posts
 3
 Rep Power
 0
Dynamic for loop
Hi,
i am trying to solve this problem where i need combinations of indexes
1. i have a number of drop down boxes. This number can change. Take it as 4 for example
2. each drop down has a number of options which can also vary. Suppose in this case
drop down 1 has 2 values
drop down 2 has 3 values
drop down 3 has 5 values
drop down 4 has 2 values
3. I need to generate permutation of all the values depending on the number of drop downs and number of options in each drop down. i need the index number combinations of all the possible combinations of drop down boxes as output.
4. output in this case needs to be
1111 , 1112, 1121, 1122, 1131, 1132, 1141, 1142, 1151, 1152, 1211, 1212, 1221, 1222, 1231, 1232... and so on.
5. i can not use nested for loop as the number of drop downs can change.
please reply.
 02072010, 07:14 PM #2Member
 Join Date
 Feb 2010
 Posts
 3
 Rep Power
 0
i had written a sample code for 3 drop downs which is working fine...i just want to make it generic
Java Code:public class Test { public static void main(String args[]) { int size = 3; int [] ls = {2,3,5}; int loop=1; int length = ls.length; int [] lInt = new int[length]; int [] vals = new int[length]; for(int i=0;i<length;i++) { loop = loop *ls[i]; lInt[i] = ls[i]; vals[i] = 1; } System.out.println("loop "+loop); for(int i=1;i<=loop;i++) { if(vals[length1]<=lInt[length1]) { for(int x=0;x<length;x++) { System.out.print(vals[x]+" "); } System.out.println(); vals[length1]++; continue; } else { vals[length1] = 1; vals[length2]++; if(vals[length2]<=lInt[length2]) { for(int x=0;x<length;x++) { System.out.print(vals[x]+" "); } System.out.println(); vals[length1]++; } } if(vals[length2]>lInt[length2]) { vals[length1]=1; vals[length2]=1; vals[length3]++; for(int x=0;x<length;x++) { System.out.print(vals[x]+" "); } System.out.println(); vals[length1]++; } } } }
 02072010, 07:36 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,820
 Blog Entries
 7
 Rep Power
 21
Maybe this helps:
Java Code:import java.util.Arrays; public class Combination { public static boolean combine(int[] c, int[] m) { for (int i= m.length; i >= 0;) if (++c[i] < m[i]) return true; else c[i]= 0; return false; } public static void main(String[] args) { int[] x= new int[3]; // all zeros to start with int[] m= { 3, 2, 4 }; // the allowed maximum values do { System.out.println(Arrays.toString(x)); } while(combine(x, m)); } }
JosLast edited by JosAH; 02072010 at 07:45 PM.
 02082010, 09:11 AM #4Member
 Join Date
 Feb 2010
 Posts
 3
 Rep Power
 0
Similar Threads

Dynamic Casting
By Thorton in forum Advanced JavaReplies: 30Last Post: 01122010, 10:01 PM 
Dynamic Classes
By nunofaria in forum Advanced JavaReplies: 8Last Post: 11172009, 07:20 AM 
Dynamic GUI
By ike2u in forum New To JavaReplies: 4Last Post: 08082009, 02:50 AM 
dynamic inherence
By itaipee in forum Advanced JavaReplies: 6Last Post: 02072009, 02:05 PM 
Dynamic DecimalFormatter
By felixtfelix in forum New To JavaReplies: 0Last Post: 03172008, 05:16 PM
Bookmarks