Hi everyone,
Just found this forum (thanks google) and was pleased to see there was a section dedicated to collections and generics..
I need some help with an iteration. I have an array of arrays, something like this:
String [][]variations = new String[6][];
Now for each of the arrays stored in this array, the length isn't known until runtime (because the values are stored from values in a database)..
At the moment, I'm testing the iterations on a set that looks something like this:
variations[0].length = 1; variations[1].length = 1; variations[2].length = 1; variations[3].length = 4; variations[4].length = 2; variations[5].length = 1;
What I am trying to achieve, is to iterate through this array of arrays, and produce a string of all possible combinations. Using the set above, there are 8 different possible combinations, something like this:
[1a][2a][3a][4a][5a][6a] [1a][2a][3a][4a][5b][6a] [1a][2a][3a][4b][5a][6a] [1a][2a][3a][4b][5b][6a] [1a][2a][3a][4c][5a][6a] [1a][2a][3a][4c][5b][6a] [1a][2a][3a][4d][5a][6a] [1a][2a][3a][4d][5b][6a]
I hope this is making sense..
Anyway, does anyone know an easy way to do this? This is the code I'm using at the moment.. It works prefectly but it's pretty messy..
for (int la = 0; la < variations[0].length; la++) { for (int lb = 0; lb < variations[1].length; lb++) { for (int lc = 0; lc < variations[2].length; lc++) { for (int ld = 0; ld < variations[3].length; ld++) { for (int le = 0; le < variations[4].length; le++) { for (int lg = 0; lg < variations[5].length; lg++) { StringBuffer sb = new StringBuffer(); sb.append(variations[0][la]); sb.append(";"); sb.append(variations[1][lb]); sb.append(";"); sb.append(variations[2][lc]); sb.append(";"); sb.append(variations[3][ld]); sb.append(";"); sb.append(variations[4][le]); sb.append(";"); sb.append(variations[5][lg]); System.out.println(sb); } } } } } } }
Thanks.