从数组中删除对象并将其替换为另一个数组中的对象

时间:2018-04-02 13:26:24

标签: java arrays oop for-loop

我遇到的问题如下:我创建了两个代表船只停靠空间的数组。第一个数组发送对象保存在数组中,如果没有空格,则将其添加到等待列表数组中。 但是当我从第一个数组中删除一个对象时,等待列表数组中的对象不会被删除并添加。

码头可容纳三种尺寸的船;货物,集装箱和超级集装箱。行由5个小型和3个中型和2个大型组成。货船(小)可以在任何可用空间停泊。集装箱船(中型)可以在中等空间和大型泊位停泊,但不能在狭小的空间内停泊。超级容器只能适应大空间。

因此,如果我输入shipName3和Super-Container,并且已经有两个Super-Container,它会添加到等待列表中,但是当我从Dock中删除一个Super-Container时,它不会从等待列表中删除一个把它添加到码头你能帮忙吗?这是我的dock方法:

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();
    System.out.println("Enter ship's size to undock: ");
    String size = scan.nextLine();
    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] != null && dock1[i].getShipName().equals(name)) {
            dock1[i] = null;
            System.out.println("Ship removed");
           //break;
            ///HERE CHECK IF SHIP IN DOCK
            for (int j = 0; j < dock1.length; j++) {
                if (dock1[j] == null) {
                    //Add ship to the dock
                    dock1[j] = new Ship(name, size);
                    System.out.println("Move ship from waiting list to dock 1");
                    break;
                } else {
                    System.out.println("No space in dock1");
                    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 < dock1.length; i++) { //waitingList?
        if (waitingList[i] == null) {
            //Add ship to the dock
            waitingList[i] = new Ship(name, size);
            System.out.println("Enough space added to waiting list");
            break;
        } else {
            System.out.println("No space on waiting list, ship turned away");
            return;
        }
    }

}

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());
        }
    }
}
}

船舶类

public class Ship {

private String shipName;
private  String shipSize;

public String getShipName() {
    return shipName;
}

public void setShipName(String shipName) {
    this.shipName = shipName;
}

public String getShipSize() {
    return shipSize;
}

public void setShipSize(String shipSize) {
    this.shipSize = shipSize;
}

public Ship(String shipName, String shipSize) {
    this.shipName = shipName;
    this.shipSize = shipSize;
}
}

我尝试了解开方法。

2 个答案:

答案 0 :(得分:1)

请检查您的取消停靠方法,因为您要从 dock1 数组中删除,然后再将相同的对象添加到 dock1 数组中没有代码从 waitingList 数组中删除对象只需在解除对接方法中更新循环,如下所述

for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] != null && dock1[i].getShipName().equals(name)) {
            dock1[i] = null;
            System.out.println("Ship removed");
            // break;
            /// HERE CHECK IF SHIP IN DOCK
            for (int j = 0; j < waitingList.length; j++) {
                if (dock1[i] == 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;
                    break;
                } else {
                    System.out.println("No space in dock1");
                    return;
                }
            }
        } else {
            System.out.println("Ship not docked here");
            break;
        }

    }

我已经更改了内循环迭代,因此它将循环到 waitingList 数组的大小,因为我们需要从 waitingList aray中删除对象并添加到 dock1 < / strong> array.Also我运行时从 waitingList 数组中获取 shipname shipize ,以便它将从 waitingList <添加对象/ strong>到 dock1 数组。我已经在我的机器上测试了代码,希望它可以帮助你。

答案 1 :(得分:0)

在您的取消停靠方法中,您可以通过将其设置为null来移除dock1数组中第i个位置的发货。这没关系,但是你有一个for循环,它遍历dock1中的所有船只,寻找一个null值的船。您知道刚删除的那个是null,因为您只需将其设置为该值。然后你将它设置为一艘新船,它具有你移除的船的确切名称和大小(基本上将船返回)。相反,您希望for循环遍历您的等待船只列表,以找到与您刚刚移除的船舶空间相匹配的船舶。

替换:

for (int j = 0; j < dock1.length; j++) {
    if (dock1[j] == null) {
        //Add ship to the dock
        dock1[j] = new Ship(name, size);
        System.out.println("Move ship from waiting list to dock 1");
        break;
     } else {
         System.out.println("No space in dock1");
         return;
     }
 }

for (int j = 0; j < waitingList.length; j++) {
    if (waitingList[j].getShipSize() <= size) {
        //Add ship to the dock
        dock1[i] = waitingList[j];
        System.out.println("Move ship from waiting list to dock 1");
        return;
    }
}
System.out.println("No space in dock1");
return;