I will have x number of lists and each list will have an arbitrary number of elements. Each element is a word. I would like to take one element from each list and build a new list that contains one word from each list. I would like to store every list and when I'm finished I will have one list for every possibility of adding the words. Let us say we have 4 lists for example:
List 0: That Is Nice
List 1: You Look Good
List 2: Awesome
List 3: How Did You Do This
One way of taking an element from each list and adding them together would be:
New List 0: That You Awesome How
or
New List 1: Is You Awesome This
How can I build a recursive algorithm that works for an arbitrary number of list with an arbitrary number of elements? Preferably I would like to solve this problem in Java.
This is what I have done so far (I haven't used counter and length yet, but I plan to use it to only get the interesting combinations):
void everyPossibleWay(HashMap<Integer, ArrayList<String>> table, ArrayList<String> everyWay, int x, int y, int length, int counter) {
if (table.get(x) != null) {
if (y < table.get(x).size()) {
everyWay.add(table.get(x).get(y));
everyPossibleWay(table, everyWay, 0, y + 1, length, counter + 1);
everyPossibleWay(table, everyWay, x + 1, y, length, counter);
}
}
else {
for (String s : everyWay)
System.out.println(s + " ");
}
}
I also know that I will get all the results in one list. But I'm just doing this to get something to work and then improve. When I run the code I only get one word from the last list.