gpt4 book ai didi

java - ArrayList 未按预期方式添加对象

转载 作者:行者123 更新时间:2023-12-01 12:25:51 24 4
gpt4 key购买 nike

我正在尝试通过 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;
}
}

关于java - ArrayList 未按预期方式添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26341050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com