我正在尝试通过ArrayList
语句向if/else
添加多个值。变量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;
}
答案 0 :(得分:1)
每次调用方法时,您都在创建一个新的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;
}
}
答案 1 :(得分:0)
您的createFleet
方法仅根据输入选项将单个船只添加到列表中。如果要添加多个发货,您可以在某个循环中运行该代码。如果你想要创建多艘船只,你不清楚为什么你首先将选择传递给方法。
如果您打算创建多个相同类型的船只,您可以这样做:
ArrayList<Ship> fleet = new ArrayList<Ship>();
for (int count=0; count < numberOfShips ; count++) {
if (choice == 1) ...
}
使用switch语句替换long if .. else if ... else if
可以进一步改进代码。
答案 2 :(得分:0)
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 ...
}