簡體   English   中英

ArrayList不會以嵌入式方式添加對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM