寻找Java中ArrayList的所有可能组合。

huangapple go评论64阅读模式
英文:

find all possible combinations of arraylists java

问题

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;
    }
}

输出结果:

[ , sprinkles, whipped cream, chocolate chips, chocolate , chocolate sprinkles, chocolate whipped cream, chocolate chocolate chips, vanilla , vanilla sprinkles, vanilla whipped cream, vanilla chocolate chips, strawberry , strawberry sprinkles, strawberry whipped cream, strawberry chocolate chips]
16

Process finished with exit code 0

希望能帮到您!

英文:

i need to find all combos of the iceCream and toppings lists and this code:

import java.util.ArrayList;

public class IceCream {

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

        ArrayList&lt;String&gt; menuList = new ArrayList&lt;String&gt;();
        long menu_num = 0;

        for (int x = 0; x &lt; iceCream.length; x++)
        {
            for (int y = 0; y &lt; toppings.length; y++)
            {
                menu_num++;
                menuList.add(iceCream[x] + &quot; &quot; +  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;
    }

            }

outputs

[   ,   sprinkles,   whipped cream,   chocolate chips, chocolate  , chocolate sprinkles, chocolate whipped cream, chocolate chocolate chips, vanilla  , vanilla sprinkles, vanilla whipped cream, vanilla chocolate chips, strawberry  , strawberry sprinkles, strawberry whipped cream, strawberry chocolate chips]
16

Process finished with exit code 0

i need to be able to have more than one topping on each ice cream and print out 32 combos total. any help is appreciated

答案1

得分: 1

如果目标是为每种冰淇淋类型找出不同的配料组合,理想的方法是使用Guava的Sets.powerset(Set set)

Sets.powerset(Set set) 将返回集合的所有子集组合。
在您的情况下,您可以生成如下的幂集:

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

// 从配料数组创建集合
Set<String> set = Sets.newHashSet(Arrays.asList(toppings));  

// powerSet 用于存储集合的所有子集
// 幂集将包含所有的配料组合作为子集
// 例如:[ , whipped cream, sprinkles], [ , whipped cream, sprinkles, chocolate chips], ...
Set<Set<String>> powerSet = Sets.powerSet(set); 

现在您有了包含子集组合的 powerSet,您可以遍历并将每个子集转换为如下的配料组合:

// 此集合用于连接子集的条目,并将其存储为字符串
Set<String> toppingsCombinationSet = Sets.newHashSet();

for (Set<String> s : powerSet) {
    String toppings = "";
    for (String topping : s) {
        toppings = toppings.trim() + " " + topping.trim();
    }
    toppingsCombinationSet.add(toppings);       
}

最后,您可以将冰淇淋类型与配料组合组合在一起,如下所示:

// 将冰淇淋类型与配料组合组合
// 需要移除空格,因为没有冰淇淋类型的值如:"sprinkles chocolate chips" 是没有意义的
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);
            }    

        }
            
    }
}
英文:

If the goal is to find out different toppings combination for each ice cream type, the ideal way is to use Guava's Sets.powerset(Set set).

Sets.powerset(Set set) will return all the combination of subsets of the set.
In your case you can generate a powerset as below,

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

// Creating a set from toppings array
Set&lt;String&gt; 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&lt;Set&lt;String&gt;&gt; powerSet = Sets.powerSet(set); 

Now that you have the powerSet with subset of combinations, you can iterate over and convert each subset to a combination of topping strings as below,

// This set is to concatinate the entries of the subset (of the powerset) and store as a string
Set&lt;String&gt; toppingsCombinationSet = Sets.newHashSet();

for (Set&lt;String&gt; s : powerSet) {
    String toppings = &quot;&quot;;
    for (String topping : s) {
        toppings = toppings.trim() + &quot; &quot; + topping.trim();
    }
    toppingsCombinationSet.add(toppings);       
}

Finally you can combine the ice cream type to the combination of toppings as below,

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

        }
            
    }
}

答案2

得分: 0

你已经接近成功了。

在你的代码中,你有以下内容:

  • 一个循环来遍历冰淇淋口味
  • 一个循环来遍历第一层配料

但是你在尝试把第二层配料加上去时遇到了问题,是吗?如果是的话,是否有意义再添加另一个循环来处理第二层配料呢?

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]);

        }

    }

}

现在,如果你运行这段代码,你会注意到有一些重复和重新排序的情况。如果你需要去除这些重复项,请告诉我。

英文:

You are almost there.

In your code, you have the following:

  • a loop to loop through the ice cream flavors
  • a loop to loop through the first layer of toppings

But you are having trouble trying to put the second topping on there, yes? If so, would it not make sense to have yet another loop for the second layer of toppings?

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

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

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

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

            }

        }

    }

Now, if you run this, you will notice that there are some duplicates and reorderings. If you need to get rid of those, let me know.

huangapple
  • 本文由 发表于 2020年9月14日 09:48:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定