I am trying to write a recursive function to get all posible combination from dynamic number of List. For example if I have 3 Lists
List 1 : {A,B}
List 2 : {B,C}
List 3 : {D}
Even though in the output each element occurs once I would like to keep the output in the structure of
List<List<List<elements>>>
My expected output will be
L1 : A, L2 : B, L3 : D
L1 : A, L2 : C, L3 : D
L1 : B, L2 : B, L3 : D
L1 : B, L2 : C, L3 : D
Here the number of list can change dynamically . So I need dynamic number of nested loop to find combinations.
Here what am I trying. Just ignore my awful code.
public List<List<List<elements>>> combinations(int depth, List<List<elements>> allLists,List<List<List<elements>>> answerList){
if(depth==allList.size())
return answerList
}
else{
for(List<element> e : answerList){
for(int j=0; j<e.size();j++){
answerList.get(depth).get(j).add(allList.get(depth).get(j));
combinations (depth+1,allLists,answerList)
}
}
}
Please help me where am I doing wrong?
EDIT:
my idea is to keep all combinations together so that
{A}
will be the deepest list in answer
{L1,L2,L3}
will be the second level of list.
{L1,L2,L3},{L1,L2,L3}
will be the outside list. So the number of Lists doest matter here. all will be covered by the above structure. my final output in the above structure is given below
{
{
{A},
{B},
{D}
},
{
{A},
{C},
{D}
},
{
{B},
{B},
{D}
},
{
{B},
{C},
{D}
}
}
>>>>`?
– Sergey Kalinichenko Nov 04 '18 at 14:06