如何在Java中向arraylist添加多个值

问题描述 投票:-1回答:1

我还是Java的新手我已经创建了许多我想用不同方法调用的Arraylists。我使用了一个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);
    ````````````````````````````````````````````````
java arraylist
1个回答
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学习好运!

© www.soinside.com 2019 - 2024. All rights reserved.