问题:在Java中合并ArrayList中的值。

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

problem combining values in arraylists java

问题

import java.util.ArrayList;

public class IceCream {
    public long printMenu() {
        int scoops[] = {0, 1};
        String iceCream[] = {" ", "chocolate", "vanilla", "strawberry"};
        String toppings[] = {" ", "sprinkles", "whipped cream", "chocolate chips"};
        ArrayList<String> menuList = new ArrayList<String>();
        long menu_num = 0;
        for (int i = 0; i < scoops.length; i++) {
            for (int x = 0; x < iceCream.length; x++) {
                for (int y = 0; y < toppings.length; y++) {
                    menu_num++;
                    if (i == 0) {
                        menuList.add(toppings[y]);
                    }
                    if (i == 1) {
                        if (toppings[y].equals(toppings[0])) {
                            menuList.add(iceCream[x]);
                        } else {
                            menuList.add(iceCream[x] + toppings[y]);
                        }
                    }
                }
            }
        }
        return menu_num;
    }

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

输出:

[ ]
[ , sprinkles]
[ , sprinkles, whipped cream]
[ , sprinkles, whipped cream, chocolate chips]
[ , chocolate]
[ , chocolate, sprinkles]
[ , chocolate, sprinkles, whipped cream]
[ , chocolate, sprinkles, whipped cream, chocolate chips]
[ , vanilla]
[ , vanilla, sprinkles]
[ , vanilla, sprinkles, whipped cream]
[ , vanilla, sprinkles, whipped cream, chocolate chips]
[ , strawberry]
[ , strawberry, sprinkles]
[ , strawberry, sprinkles, whipped cream]
[ , strawberry, sprinkles, whipped cream, chocolate chips]
[ , sprinkles]
[ , sprinkles, sprinkles]
[ , sprinkles, sprinkles, whipped cream]
[ , sprinkles, sprinkles, whipped cream, chocolate chips]
[ , chocolate]
[ , chocolate, sprinkles]
[ , chocolate, sprinkles, whipped cream]
[ , chocolate, sprinkles, whipped cream, chocolate chips]
[ , vanilla]
[ , vanilla, sprinkles]
[ , vanilla, sprinkles, whipped cream]
[ , vanilla, sprinkles, whipped cream, chocolate chips]
[ , strawberry]
[ , strawberry, sprinkles]
[ , strawberry, sprinkles, whipped cream]
[ , strawberry, sprinkles, whipped cream, chocolate chips]
英文:

i'm trying to combine all possible combinations of the strings i have in the iceCream and toppings string lists. But this:

import java.util.ArrayList;
public class IceCream {
public long printMenu( ) {
int scoops[] = {0, 1};
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 i = 0; i &lt; scoops.length; i++) {
for (int x = 0; x &lt; iceCream.length; x++) {
for (int y = 0; y &lt; toppings.length; y++) {
menu_num++;
if (i == 0) {
menuList.add(toppings[y]);
}
if (i == 1) {
if (toppings[y] == toppings[0]) {
menuList.add(iceCream[x]);
}
else {
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( );
assert count == 32;
}
}

outputs

[ ]
[ , sprinkles]
[ , sprinkles, whipped cream]
[ , sprinkles, whipped cream, chocolate chips]
[ , sprinkles, whipped cream, chocolate chips,  ]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla, vanillasprinkles]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla, vanillasprinkles, vanillawhipped cream]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla, vanillasprinkles, vanillawhipped cream, vanillachocolate chips]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla, vanillasprinkles, vanillawhipped cream, vanillachocolate chips, strawberry]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla, vanillasprinkles, vanillawhipped cream, vanillachocolate chips, strawberry, strawberrysprinkles]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla, vanillasprinkles, vanillawhipped cream, vanillachocolate chips, strawberry, strawberrysprinkles, strawberrywhipped cream]
[ , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  , sprinkles, whipped cream, chocolate chips,  ,  sprinkles,  whipped cream,  chocolate chips, chocolate, chocolatesprinkles, chocolatewhipped cream, chocolatechocolate chips, vanilla, vanillasprinkles, vanillawhipped cream, vanillachocolate chips, strawberry, strawberrysprinkles, strawberrywhipped cream, strawberrychocolate chips]
Process finished with exit code 0

i can't figure out why it's printing the toppings over and over again and i just want a list with all combos of the flavors and toppings. any help is appreciated

答案1

得分: 0

import java.util.ArrayList;

public class IceCream {

    public long printMenu( ) 
    {
        int scoops[] = {0, 1};
        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;
    }
}
英文:
import java.util.ArrayList;
public class IceCream {
public long printMenu( ) 
{
int scoops[] = {0, 1};
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;
}

}

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

发表评论

匿名网友

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

确定