Lets say I have 3 lists with 3 elements each.
List1: "cat, sat, mat"; List2: "every, boy, deserves; List3: all, lines, here
. My output should be:
Listout: cat,every,all; cat,every,lines; cat,every,here; cat,boy,all; cat,boy,lines;..
I can write a method that can append all elements of first while there is a loop that runs through the two other lists. But how to tackle this for more than 3 lists. Like 10 lists. The output will contain 3 to the 10 elements. Can you give me an idea of how the code/method in Java would look like? I know I might need recursion: but what would be the input to that recursive method?
I have tried this one like this and it works:
public static LinkedList<String> getPermutations(LinkedList<String> list1, LinkedList<String> list2, LinkedList<String> list3){
LinkedList<String> final_list = new LinkedList<String>();
Iterator<String> it = list1.iterator();
while (it.hasNext()) {
String this_element1 = it.next();
//System.out.println("elem1: "+this_element1);
Iterator<String> it2 = list2.iterator();
while (it2.hasNext()) {
String this_element2 = it2.next();
//System.out.println("elem2: "+this_element2);
Iterator<String> it3 = list3.iterator();
while (it3.hasNext()) {
String this_element3 = it3.next();
//System.out.println(this_element3);
final_list.add(this_element1+","+this_element2+","+this_element3);
}//3
}//2
}//1
return final_list;
}