英文:
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[] = {" ", "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;
}
}
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[] = {" ", "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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论