查找ArrayList和java的所有可能组合

查找ArrayList和java的所有可能组合,java,arraylist,Java,Arraylist,我需要找到所有冰激凌和配料列表的组合以及以下代码: import java.util.ArrayList; public class IceCream { public long printMenu( ) { String iceCream[] = {" ", "chocolate", "vanilla", "strawberry"}; String topping

我需要找到所有冰激凌和配料列表的组合以及以下代码:

import java.util.ArrayList;

public class IceCream {

    public long printMenu( )
    {
        String iceCream[] = {" ", "chocolate", "vanilla", "strawberry"};
        String toppings[] = {" ", "sprinkles", "whipped cream", "chocolate chips"};

        ArrayList<String> menuList = new ArrayList<String>();
        long menu_num = 0;

        for (int x = 0; x < iceCream.length; x++)
        {
            for (int y = 0; y < toppings.length; y++)
            {
                menu_num++;
                menuList.add(iceCream[x] + " " +  toppings[y]);
            }
        }

        System.out.println(menuList);
        return menu_num;
    }

    public static void main( String [ ] args ) {
        IceCream obj = new IceCream( );
        long count = obj.printMenu( );
        System.out.println(count);
        assert count == 32;
    }

            }
我需要能够有一个以上的顶部对每个冰淇淋和打印出32个组合总数。感谢您的帮助

您就快到了

在代码中,您有以下内容:

  • 一圈一圈地品尝冰淇淋的味道
  • 一个环到一个环穿过第一层浇头
但是您在尝试将第二个浇头放在那里时遇到了困难,是吗?如果是这样的话,那么在第二层浇头上再加一个圈是否有意义呢

    for (int x = 0; x < iceCream.length; x++)
    {

        for (int y = 0; y < toppings.length; y++)
        {

            for (int z = 0; z < toppings.length; z++)
            {

                menu_num++;
                menuList.add(iceCream[x] + " " +  toppings[y] + " " toppings[z]);

            }

        }

    }
for(int x=0;x

现在,如果你运行这个,你会注意到有一些重复和重新排序。如果您需要去除这些,请告诉我。

如果目标是为每种冰淇淋找到不同的配料组合,理想的方法是使用番石榴

集合。powerset(Set Set)将返回集合的所有子集组合。 在您的情况下,您可以生成如下所示的功率集

String toppings[] = {" ", "sprinkles", "whipped cream", "chocolate chips"};

// Creating a set from toppings array
Set<String> set = Sets.newHashSet(Arrays.asList(toppings));  

// powerSet to store all subsets of a set 
// The powerset will contain all the combination of toppings as subsets
// Ex: [ , whipped cream, sprinkles], [ , whipped cream, sprinkles, chocolate chips], ...
Set<Set<String>> powerSet = Sets.powerSet(set); 
// This set is to concatinate the entries of the subset (of the powerset) and store as a string
Set<String> toppingsCombinationSet = Sets.newHashSet();

for (Set<String> s : powerSet) {
    String toppings = "";
    for (String topping : s) {
        toppings = toppings.trim() + " " + topping.trim();
    }
    toppingsCombinationSet.add(toppings);       
}
// Combine the ice Cream to the toppings combination
// Whitespaces need to be removed as it doesn't make any sense to have values like : "sprinkles chocolate chips" (without ice cream type)
for (int x = 0; x < iceCream.length; x++) { 
    if (!" ".equals(iceCream[x])) {
        for (String topping : toppingsCombinationSet) {
            if (!" ".equals(topping)) {
                String s = (iceCream[x].trim() + " " +  topping.trim()).trim();
                menuList.add(s);
            }    

        }
            
    }
}
stringtoppings[]={”、“Spreads”、“whipped cream”、“chocolate chips”};
//从浇头数组创建集合
Set Set=Sets.newHashSet(Arrays.asList(toppings));
//用于存储集合的所有子集的powerSet
//动力集将包含作为子集的所有浇头组合
//例:[,鲜奶油,洒点],,鲜奶油,洒点,巧克力片]。。。
设置功率集=组。功率集(组);
现在您有了带有组合子集的powerSet,您可以迭代每个子集并将其转换为顶级字符串的组合,如下所示

String toppings[] = {" ", "sprinkles", "whipped cream", "chocolate chips"};

// Creating a set from toppings array
Set<String> set = Sets.newHashSet(Arrays.asList(toppings));  

// powerSet to store all subsets of a set 
// The powerset will contain all the combination of toppings as subsets
// Ex: [ , whipped cream, sprinkles], [ , whipped cream, sprinkles, chocolate chips], ...
Set<Set<String>> powerSet = Sets.powerSet(set); 
// This set is to concatinate the entries of the subset (of the powerset) and store as a string
Set<String> toppingsCombinationSet = Sets.newHashSet();

for (Set<String> s : powerSet) {
    String toppings = "";
    for (String topping : s) {
        toppings = toppings.trim() + " " + topping.trim();
    }
    toppingsCombinationSet.add(toppings);       
}
// Combine the ice Cream to the toppings combination
// Whitespaces need to be removed as it doesn't make any sense to have values like : "sprinkles chocolate chips" (without ice cream type)
for (int x = 0; x < iceCream.length; x++) { 
    if (!" ".equals(iceCream[x])) {
        for (String topping : toppingsCombinationSet) {
            if (!" ".equals(topping)) {
                String s = (iceCream[x].trim() + " " +  topping.trim()).trim();
                menuList.add(s);
            }    

        }
            
    }
}
//此集合包含(电源集)子集的条目并存储为字符串
Set-toppingsCombinationSet=Sets.newHashSet();
用于(组s:功率集){
线头=”;
用于(字符串顶部:s){
浇头=浇头.trim()+“”+浇头.trim();
}
浇头组合集。添加(浇头);
}
最后,您可以将冰淇淋类型与配料组合如下:

String toppings[] = {" ", "sprinkles", "whipped cream", "chocolate chips"};

// Creating a set from toppings array
Set<String> set = Sets.newHashSet(Arrays.asList(toppings));  

// powerSet to store all subsets of a set 
// The powerset will contain all the combination of toppings as subsets
// Ex: [ , whipped cream, sprinkles], [ , whipped cream, sprinkles, chocolate chips], ...
Set<Set<String>> powerSet = Sets.powerSet(set); 
// This set is to concatinate the entries of the subset (of the powerset) and store as a string
Set<String> toppingsCombinationSet = Sets.newHashSet();

for (Set<String> s : powerSet) {
    String toppings = "";
    for (String topping : s) {
        toppings = toppings.trim() + " " + topping.trim();
    }
    toppingsCombinationSet.add(toppings);       
}
// Combine the ice Cream to the toppings combination
// Whitespaces need to be removed as it doesn't make any sense to have values like : "sprinkles chocolate chips" (without ice cream type)
for (int x = 0; x < iceCream.length; x++) { 
    if (!" ".equals(iceCream[x])) {
        for (String topping : toppingsCombinationSet) {
            if (!" ".equals(topping)) {
                String s = (iceCream[x].trim() + " " +  topping.trim()).trim();
                menuList.add(s);
            }    

        }
            
    }
}
//将冰淇淋与配料组合在一起
//需要删除空白,因为像“洒巧克力片”(不含冰淇淋类型)这样的值没有任何意义
对于(intx=0;x
问题是什么?我想不出如何在阵列中为每种口味的冰淇淋添加多个配料。您已经打印了16种可能的配料组合。若你们把它和四种口味的冰淇淋(包括空白的)结合起来,那个就是4x16=64A)我不会用空格作为不同的名字,这是很难正确阅读的。为什么不称之为默认奶油/浇头之类的东西呢。4种奶油,4种不同的配料。所以你得到4 X 4。。。16个组合?!为什么期望32个?@Tarik no,Alex打印了4 x 4个奶油和配料组合。冰淇淋类型已经是输出的一部分。不知道为什么亚历克斯期望32对,或者你认为应该是64对。哪里说目标是两个冠军?触球。最初的海报要求超过1个浇头,所以我提供了一种实现这一目标的方法。