【问题标题】:Removing object from array and replacing it with object from another array从数组中删除对象并将其替换为另一个数组中的对象
【发布时间】:2018-09-11 17:25:55
【问题描述】:

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

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

因此,例如,如果我输入 shipName3 和 Super-Container,并且已经有两个 Super-Container,它会添加到等候名单中,但是当我从码头移除一个 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;
}
}

我尝试使用 undock 方法。

【问题讨论】:

  • 错误方法名在哪里?
  • 问题出在 undock 类中,但没有错误消息,只是没有移动对象
  • @S.smith94 当您将从数组中删除对象时,如果等待列表中存在对象并且数组中有空间,则必须将等待列表中的元素添加到数组中?如果我错了,请纠正我
  • @S.smith94 同时你必须从等候名单中删除对象
  • 是的,我需要将它从等待列表数组中删除并添加到 dock1 数组中

标签: java arrays oop for-loop


【解决方案1】:

请检查您的 undock 方法,因为您正在从 dock1 数组中删除,并且再次将相同的对象添加到 dock1 数组中,但存在没有从 waitingList 数组中删除对象的代码,只需在 undock 方法中更新您的循环,如下所述

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 数组中删除对象并添加到 dock1 数组。我也在运行时从 waitingList 数组中获取 shipnameshipsize,以便它会从 waitingList 到 dock1 数组。我已经在我的机器上测试了代码,希望它会对你有所帮助。

【讨论】:

  • @S.smith94 检查数组中的元素
【解决方案2】:

在您的 undock 方法中,您通过将其设置为 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;

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 2020-04-28
    • 2021-12-13
    • 1970-01-01
    • 2019-11-23
    • 2023-01-26
    • 2015-06-19
    • 1970-01-01
    相关资源
    最近更新 更多