[英]How to add multiple values to an arraylist in Java
I'm still new to Java I have created a number of Arraylists I want to call from different methods. 我对Java还是很陌生,我创建了许多要从不同方法调用的Arraylist。 I have used a while loop to run the code a number of times. 我使用了while循环多次运行代码。 I was wondering if someone could help me fix my problem. 我想知道是否有人可以帮助我解决我的问题。 A user should be able to add as many pizza's as he/she wishes before the code stops running. 用户应该能够在代码停止运行之前添加任意数量的比萨饼。
public static void main(String[] args) {
boolean test = true;
while(test == true){
System.out.println("Please press 1 to start a new order, 2 to update an order or 3 to delete 0 to finish");
Scanner inputValue = new Scanner(System.in);
String value = inputValue.nextLine();
int result = Integer.parseInt(value);
Orders order = new Orders();
int number = result;
if (number == 1){
order.add();
}
if (number == 2){
order.update();
}
if(number == 3){
order.delete();
}
if(number == 0){
order.calculatePizzas();
test = false;
}
}
}
order.add() order.add()
public ArrayList <String> pizzaSizeA = new ArrayList<>();
public ArrayList<Double> sizePriceA = new ArrayList<>();
public ArrayList<String> crustName = new ArrayList<>();
public ArrayList<Double> crustPrice = new ArrayList<>();
public ArrayList<Double> base = new ArrayList<>();
public ArrayList<String> sauceName = new ArrayList<>();
public ArrayList<Double> saucePrice = new ArrayList<>();
public ArrayList<String> topping1 = new ArrayList<>();
public ArrayList<Double> t1Price = new ArrayList<>();
public ArrayList <String> topping2 = new ArrayList<>();
public ArrayList<Double> t2Price = new ArrayList<>();
public ArrayList<Integer> Quantity = new ArrayList<>();
// Add the pizzas so that they get added to the Array.
System.out.println("Please enter a Base?");
Scanner baseSize = new Scanner(System.in);
String baseSize1 = baseSize.nextLine();
JavaApplication4.Pizza Basesize1 = new JavaApplication4.Pizza();
Basesize1.Size(baseSize1);
pizzaSizeA.add(baseSize1);
//Enter the crust you would like
System.out.println("\n Please enter your crust?");
Scanner crustty = new Scanner(System.in);
String crustt = crustty.nextLine();
JavaApplication4.Pizza crustPizza = new JavaApplication4.Pizza();
JavaApplication4.Pizza.crust(crustt);
crustName.add(crustt);
// calculate the base price
JavaApplication4.Pizza basePrice = new JavaApplication4.Pizza();
JavaApplication4.Pizza.base();
//Sauce price
System.out.println("Please enter a Sauce?");
Scanner cSauce = new Scanner(System.in);
String cSauce1 = cSauce.nextLine();
JavaApplication4.Pizza csauce = new JavaApplication4.Pizza();
JavaApplication4.Pizza.sauce(cSauce1);
sauceName.add(cSauce1);
//Choose topping
System.out.println("Choose a topping you would like to add?");
Scanner top = new Scanner(System.in);
String toppin = top.nextLine();
JavaApplication4.Pizza to0pping = new JavaApplication4.Pizza();
JavaApplication4.Pizza.topping1(toppin);
topping1.add(toppin);
//Choose topping 2
System.out.println("Choose your second topping?");
Scanner top2 = new Scanner(System.in);
String toppin2 = top2.nextLine();
JavaApplication4.Pizza to0pping2 = new JavaApplication4.Pizza();
JavaApplication4.Pizza.topping2(toppin2);
topping2.add(toppin2);
//Quantity
System.out.println("Please enter quantity");
Scanner qunatity = new Scanner(System.in);
String quan = qunatity.nextLine();
int q = Integer.parseInt(quan);
Orders.this.Quantity.add(q);
System.out.println(Quantity);
````````````````````````````````````````````````
I think it will suit you: 我认为它将适合您:
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
And you can do this: 您可以这样做:
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
myArray.add(stooges);
And such a trick: 和这样的把戏:
myArray.add(Arrays.asList("Larry", "Moe", "Curly"););
Here is a link to some very useful code examples. 这里是一些非常有用的代码示例的链接。 Good examples help a lot to learn. 好的例子对学习有很大帮助。
https://github.com/in28minutes/java-a-course-for-beginners https://github.com/in28minutes/java-a-course-for-beginners
Good luck with your Java learning! 祝您学习Java顺利!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.