如何在Java中向数组列表添加多个值

时间:2019-04-25 10:49:28

标签: java arraylist

我还是Java的新手,我创建了许多要从不同方法调用的Arraylist。我使用了while循环多次运行代码。我想知道是否有人可以帮助我解决我的问题。 用户应该能够在代码停止运行之前添加任意数量的比萨饼。

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()

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

1 个答案:

答案 0 :(得分:1)

我认为它将适合您:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

您可以执行以下操作:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
myArray.add(stooges);

这招:

myArray.add(Arrays.asList("Larry", "Moe", "Curly"););

这里是一些非常有用的代码示例的链接。好的例子对学习有很大帮助。

https://github.com/in28minutes/java-a-course-for-beginners

祝您学习Java顺利!