如何在ArrayList Java中添加另一个推理

时间:2016-12-11 13:19:47

标签: java arraylist

因此,当我运行代码时,我能够运行所有查找。当我添加另一个平面的信息,并在最后进行for循环显示。它会显示我输入的飞机数量,但会将所有数据更改回最新数据。

示例:我创建了三架不同的飞机,一旦完成,我退出循环,但这三架飞机上的所有信息都变为我放入数据的最后一架飞机。

我是否必须在myAl.add()的循环中添加一个计数器;还是有另一种方法可以做到这一点吗?

public static void Airplane() {

  String choice = "n";  

    ArrayList<Airplane> myAl = new ArrayList();

    do
    {
       Airplane xx = new Airplane();

       // making the airplane
        System.out.println("Please enter the Make of the Airplane:");
       xx.SetMake(myScan.nextLine());
        System.out.println("Please enter the Model of the Airplane: ");
       xx.SetModel(myScan.nextLine());
       System.out.println("Please enter the Weight of the Airplane: ");
       xx.SetWeight(Integer.parseInt(myScan.nextLine()));
       System.out.println("Please enter the Horse Power of the Airplane: ");
       xx.SetHorsePower(Integer.parseInt(myScan.nextLine()));
        System.out.println("Please enter the Color of the Airplane: ");
        xx.SetColor(myScan.nextLine());
       myAl.add(xx);


        System.out.println("Would you like to add another plane? \n Y/N");
        choice = myScan.nextLine();

    }
    while(!choice.equals("n"));

   Airplane spawn = new Airplane();

   for(int cntr = 0; cntr < myAl.size(); cntr++)
   {
       spawn = myAl.get(cntr);
       System.out.println(spawn.Combine());
   }

}

0 个答案:

没有答案