I have an algorithm that returns all possible combinations by taking one item from each column (Here a choice of soup, noodles and toppings).
Is there a more efficent and dynamic way to do this? For the findAllCombinations method to work, I need to know how many columns there are and hard code them.
Valid Combinations: [Watercress Soup, Udon, Fish Cube], [Spicy Soup, Ramen, Ham],...
ArrayList<ArrayList<String>> listOfLists = Lists.newArrayList();
listOfLists.add(Lists.newArrayList("Original Soup", "Spicy Soup", "Watercress Soup", "Thai Spicy Soup", "Malaysia Spicy Soup"));
listOfLists.add(Lists.newArrayList("Udon", "Ramen", "Egg Noodle", "Flat Rice Noodle", "Vermicelli", "Instant Noodle"));
listOfLists.add(Lists.newArrayList("Fish Cube", "Fish Ball", "Ham", "Squid", "Seaweed"));
ArrayList<ArrayList<String>> combo = findAllCombinations(listOfLists);
private ArrayList<ArrayList<String>> findAllCombinations(ArrayList<ArrayList<String>> arrays){
ArrayList<ArrayList<String>> combinations = new ArrayList<>();
for(String item1: arrays.get(0)){
for(String item2: arrays.get(1)){
for(String item3: arrays.get(2)){
ArrayList<String> temp = new ArrayList<String>() {
{
add(item1);
add(item2);
add(item3);
}
};
combinations.add(temp);
}
}
}
return combinations;
}