arraylists java中的值组合问题
我正在尝试将冰淇淋和配料串列表中所有可能的串组合起来。但这是:arraylists java中的值组合问题,java,arraylist,Java,Arraylist,我正在尝试将冰淇淋和配料串列表中所有可能的串组合起来。但这是: import java.util.ArrayList; public class IceCream { public long printMenu( ) { int scoops[] = {0, 1}; String iceCream[] = {" ", "chocolate", "vanilla", "strawberr
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] == 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;
}
}
我不明白为什么它会一次又一次地打印配料,我只想要一个包含所有口味和配料组合的列表。感谢您的帮助import java.util.ArrayList;
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;
}
公共级冰淇淋{
公共长打印菜单()
{
int scoops[]={0,1};
串冰淇淋[]={“,”巧克力“,”香草“,”草莓“};
串状浇头[]={“,”洒“,”搅打奶油“,”巧克力片“};
ArrayList menuList=新的ArrayList();
长菜单_num=0;
用于(int x=0;x
}导入java.util.ArrayList;
公共级冰淇淋{
公共长打印菜单()
{
int scoops[]={0,1};
串冰淇淋[]={“,”巧克力“,”香草“,”草莓“};
串状浇头[]={“,”洒“,”搅打奶油“,”巧克力片“};
ArrayList menuList=新的ArrayList();
长菜单_num=0;
用于(int x=0;x
}如何制作,使每种口味都有多个配料?如何制作,使每种口味都有多个配料?
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;
}