只能从数组中的索引0中删除对象

时间:2018-04-04 15:59:42

标签: java arrays oop

我遇到的问题如下:我创建了两个代表船只停靠空间的数组。第一个数组(dock1 [])ship对象(shipName和size - 通常是Super-Container)可以保存在数组中。如果我想从dock1 []中删除对象,我输入shipName将其删除。

但是我只能从数组中的第一个空格(索引0)中删除ship对象,而不能从任何其他空间即索引1,2,3中删除。

你能帮忙吗?这是我的dock类,在undock()if语句中的问题:

import java.util.*;

public class Main {

static Scanner scan = new Scanner(System.in);

private static Ship[] dock1 = new Ship[10];
private static Ship[] waitingList = new Ship[10];

public static void main(String[] args) {
    menu();
}

public static void menu() {


    Scanner scan = new Scanner(System.in);


    while (true) {

        System.out.println("Choose an option: 1-3");
        System.out.println("1. Dock");
        System.out.println("2. Undock");
        System.out.println("3. Status");

        int menu = scan.nextInt();
        switch (menu) {
            case 1:
                System.out.println("1. Dock");
                dock();
                break;
            case 2:
                System.out.println("2. Undock");
                undock();
                break;
            case 3:
                System.out.println("3. Status");
                printDock();
                printWaitingList();
                break;
            case 4:
                System.out.println("4. Exit");
                System.exit(0);
            default:
                System.out.println("No such option");
                break;
        }
    }
}


public static void dock() {

    System.out.println("Enter ship's name: ");
    String name = scan.nextLine();

    System.out.println("Enter ship's size: ");
    String size = scan.nextLine();

    System.out.println("Enter the ships dock:");
    //Check if the dock number is valid
    int i = Integer.valueOf(scan.nextLine());
    if (i >= 0 && i < 10 && dock1[i] == null) {
        int c = 0;
        int co = 0;
        int sco = 0;
        for (int j = 0; j < dock1.length; j++) {
            if (dock1[j] != null && dock1[j].getShipSize().equals("Cargo")) {
                c++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Container")) {
                co++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Super-Container")) {
                sco++;
            }
        }

        if (c < 10 && co < 5 && sco < 2) {
            //Add ship to the dock
            dock1[i] = new Ship(name, size);
            System.out.println("Enough space you can dock");
            System.out.println("Ship has been docked");
        } else {
            System.out.println("You cannot dock");
            waitingList(name, size);
        }

    } else {
        System.out.println("Couldn't dock");
        waitingList(name, size);
    }

}


public static void undock() {
    System.out.println("Status of ships: ");
    printDock();
    System.out.println("Enter ship's name to undock: ");
    String name = scan.nextLine();

    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
            dock1[i] = null;
            System.out.println("Ship removed");
            /// HERE CHECK IF SHIP IN DOCK
            for (int j = 0; j < waitingList.length; j++) {
                if (dock1[i] == null && waitingList[j] != null) {
                    // Add ship to the dock
                    dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                    System.out.println("Move ship from waiting list to dock 1");
                    waitingList[j] = null;
                    return;
                } else {
                 //   System.out.println("No space in dock");
                    return;
                }
            }
        } else {
            System.out.println("Ship not docked here");
            break;
        }

    }

}

public static void waitingList(String name, String size) {

    System.out.println("Dock 1 is full, ship will try to be added to Waiting List");
    for (int i = 0; i < waitingList.length; i++) {
        if (waitingList[i] == null) {
            //Add ship to the dock
            waitingList[i] = new Ship(name, size);
            System.out.println("Enough space added to waiting list");
            return;
        } else {

        }
    }
    System.out.println("No space on waiting list, ship turned away.");
}

public static void printDock() {

    System.out.println("Docks:");

    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + dock1[i].getShipName() + " " + dock1[i].getShipSize());
        }
    }
}

private static void printWaitingList() {

    System.out.println("Waiting List:");

    for (int i = 0; i < waitingList.length; i++) {
        if (waitingList[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + waitingList[i].getShipName() + " " + waitingList[i].getShipSize());
        }
    }
}
}

2 个答案:

答案 0 :(得分:2)

问题在于,只要船舶没有停靠在第一个位置(索引0),您就不会检查其他位置,因为如果它不等于要取消停靠的船舶的名称,则会有一个break语句。 break语句终止循环,不会继续检查其他位置。

只需在取消停靠方法中删除break语句。

修改

您的代码应该是这样的。

 System.out.println("Status of ships: ");
printDock();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
boolean deleted = false;
for (int i = 0; i < dock1.length; i++) {
    if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
        dock1[i] = null;
        System.out.println("Ship removed");
        deleted = true;
        /// HERE CHECK IF SHIP IN DOCK
        for (int j = 0; j < waitingList.length; j++) {
            if (dock1[i] == null && waitingList[j] != null) {
                // Add ship to the dock
                dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                System.out.println("Move ship from waiting list to dock 1");
                waitingList[j] = null;
                return;
            } else {
                //   System.out.println("No space in dock");
                return;
            }
        }
    }

}
if (!deleted) System.out.println("No ship was removed")

答案 1 :(得分:1)

我看到了2个错误:

1)你在undock方法的else语句中打破了循环。 2)如果您在第一个停靠栏中找到了船名,那么您总是在waitingList循环的第一次迭代中返回。

for (int i = 0; i < dock1.length; i++) {
    if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
        dock1[i] = null;
        System.out.println("Ship removed");
        /// HERE CHECK IF SHIP IN DOCK
        for (int j = 0; j < waitingList.length; j++) {
            if (dock1[i] == null && waitingList[j] != null) {
                // Add ship to the dock
                dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                System.out.println("Move ship from waiting list to dock 1");
                waitingList[j] = null;
                return;
            } else {
             //   System.out.println("No space in dock");
                return;
            }
        }
        // NOTE -> THIS ALWAYS ENDS IN A RETURN
    } else {
        System.out.println("Ship not docked here");
        break;
    }
}

我认为你应该省略break声明以尝试其他码头。另外,在测试等待列表时不要返回调用方法。

所以试试这个:

for (int i = 0; i < dock1.length; i++) {
    if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
                      dock1[i] = null;
        System.out.println("Ship removed");
        /// HERE CHECK IF SHIP IN DOCK
        for (int j = 0; j < waitingList.length; j++) {
            if (dock1[i] == null && waitingList[j] != null) {
                // Add ship to the dock
                dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                System.out.println("Move ship from waiting list to dock 1");
                waitingList[j] = null;
                return;
            } else {
             //   System.out.println("No space in dock, go on in waiting list");
              // NO RETURN HERE
            }
        }
    } else {
        System.out.println("Ship not docked here, try next dock if there is one left");
        // NO BREAK HERE
    }
}
System.out.println("Ship not docked in any dock");