[英]ArrayList doesn't add objects the inteded way
我试图通过if/else
语句将多个值添加到ArrayList
。 变量numberOfShips
声明作为一个字段的数量。 但是,当我打印出.size()
,大小仅为1,最后添加的对象就是其中的一个。 我不知道我在做什么错。 我知道ArrayList
会随着元素的添加而隐式增加,所以不可能。
public ArrayList<Ship> createFleet(int choice) {
ArrayList<Ship> fleet = new ArrayList<Ship>();
if (count < numberOfShips && choice > 0 && choice < 5) {
if (choice == 1) {
Ship ac = new Ship("Aircraft carrier", 5, false);
fleet.add(ac);
count++;
System.out.println("Aircraft carrier has been added to fleet.");
} else if (choice == 2) {
Ship bs = new Ship("Battleship", 4, false);
fleet.add(bs);
count++;
System.out.println("Battleship has been added to fleet.");
} else if (choice == 3) {
Ship sm = new Ship("Submarine", 3, false);
fleet.add(sm);
count++;
System.out.println("Submarine has been added to fleet.");
} else if (choice == 4) {
Ship ds = new Ship("Destroyer", 3, false);
fleet.add(ds);
count++;
System.out.println("Destroyer has been added to fleet.");
} else if (choice == 5) {
Ship sp = new Ship("Patrol Boat", 2, false);
fleet.add(sp);
count++;
System.out.println("Patrol boat has been added to fleet.");
}
} else {
System.out.println("Not an option.");
}
return fleet;
}
每次调用该方法时,您都在创建一个新的ArrayList
。 您需要在方法之外维护一个ArrayList
。
public ArrayList<Ship> createFleet(int choice) {
ArrayList<Ship> fleet = new ArrayList<Ship>(); //Here you create a new ArrayList and return it with a single Ship in it.
您需要一个全局范围的ArrayList
变量:
private List<Ship> fleet = new ArrayList<Ship>();
public ArrayList<Ship> createFleet(int choice) {
if (count < numberOfShips && choice > 0 && choice < 5) {
if (choice == 1) {
Ship ac = new Ship("Aircraft carrier", 5, false);
fleet.add(ac);
count++;
System.out.println("Aircraft carrier has been added to fleet.");
} else if (choice == 2) {
Ship bs = new Ship("Battleship", 4, false);
fleet.add(bs);
count++;
System.out.println("Battleship has been added to fleet.");
} else if (choice == 3) {
Ship sm = new Ship("Submarine", 3, false);
fleet.add(sm);
count++;
System.out.println("Submarine has been added to fleet.");
} else if (choice == 4) {
Ship ds = new Ship("Destroyer", 3, false);
fleet.add(ds);
count++;
System.out.println("Destroyer has been added to fleet.");
} else if (choice == 5) {
Ship sp = new Ship("Patrol Boat", 2, false);
fleet.add(sp);
count++;
System.out.println("Patrol boat has been added to fleet.");
}
} else {
System.out.println("Not an option.");
}
return fleet;
}
我的建议是创建一个Fleet
类,其中包含ArrayList
的引用:
public class Fleet {
private List<Ship> internalFleet = new ArrayList<Ship>();
private static int MAX_SHIPS = 10;
public void addShip(int choice){
if (internalFleet.size() < MAX_SHIPS) {
Ship ship;
switch(choice){
case 1: ship = new Ship("Aircraft carrier", 5, false);
break;
case 2: ship = new Ship("Battleship", 4, false);
break;
case 3: ship = new Ship("Submarine", 3, false);
break;
case 4: ship = new Ship("Destroyer", 3, false);
break;
case 5: ship = new Ship("Patrol Boat", 2, false);
break;
default: System.out.println("Not an option.");
}
if(ship!=null){
internalFleet.add(ship);
System.out.println(ship.getName() + " has been added to fleet.");
}
}
}
public ArrayList<Ship> getFleet(){
return internalFleet;
}
}
根据输入的选择,您的createFleet
方法仅将单个飞船添加到列表中。 如果要添加多艘船,则将在某些循环中运行该代码。 目前尚不清楚,如果要创建多个船舶,为什么要首先将选择权传递给方法。
如果您打算创建多个相同类型的飞船,则可以执行以下操作:
ArrayList<Ship> fleet = new ArrayList<Ship>();
for (int count=0; count < numberOfShips ; count++) {
if (choice == 1) ...
}
通过将long if .. else if ... else if
替换为switch语句,可以进一步改善代码。
for(int count=0; count < numberOfShips ; count++){
if (choice == 1) {
fleet.add(new Ship("Aircraft carrier", 5, false));
System.out.println("Aircraft carrier has been added to fleet.");
}
else if ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.