简体   繁体   English

如何在此ArrayList中搜索null,然后将其替换为另一个Object?

[英]How do I search for null in this ArrayList and then replace it with another Object?

I have been writing code of a car parking structure for a bit now and i've gotten kinda stuck. 我已经写了一段时间的停车场结构代码,有点卡住了。 So far I have an ArrayList that the user adds Vehicle properties to aswell as an ArrayList for a parking space with a "SpaceID" and the "Vehicle" from earlier. 到目前为止,我有一个ArrayList,用户可以将Vehicle属性添加到一个停车位的ArrayList中,而该ArrayList带有前面的“ SpaceID”和“ Vehicle”。

So far I can make it so that the user adds the vehicle, and the vehicle gets added to a parking space, However I have made a temporary parking space with the index 0: and the element (vehicle) being null. 到目前为止,我可以做到这样,以便用户添加车辆并将车辆添加到停车位,但是,我已经建立了一个索引为0:且元素(车辆)为空的临时停车位。

From here, I wanted to check the parking space ArrayList for "null" and then if it's found, replace null with the vehicle, However, I do not know how to go about implementing this. 从这里开始,我想检查停车位ArrayList是否为“ null”,然后如果找到它,则将车辆替换为null,但是,我不知道如何实现此目的。 I will attach the current code i'm using, or at least a minimal version of it. 我将附加我正在使用的当前代码,或至少附加它的最小版本。

I have already tried using a Contains(null) method, but I can't seem to get that to work properly, I'm not sure if it even can work, but I have since then removed it from my code. 我已经尝试过使用Contains(null)方法,但是我似乎无法使其正常工作,我不确定它是否还能工作,但是从那以后我就从我的代码中删除了它。

First of all, I have the function to create the vehicle and store it in an Array. 首先,我具有创建车辆并将其存储在Array中的功能。

```
public void carInfo(Vehicle tempVehicle, parkingSpace vehicle) {

    array = new MCP();

    System.out.println("Please enter number plate: ");
    input = new Scanner(System.in);
    String plate = input.nextLine();

    System.out.println("Please enter car make: ");
    input = new Scanner(System.in);
    String make = input.nextLine();

    System.out.println("How would you best describe your Vehicle? ");
    System.out.println("(Car, Small Van, Tall Van, Long Van, Coach, 
    Motorbike)");
    type = new Scanner(System.in);
    String type1 = input.nextLine();

    if (type1.equalsIgnoreCase(vehicleType.CAR.toString())) {
        tempVehicle.setPlate(plate);
        tempVehicle.setCarMake(make);
        tempVehicle.setVehicle(vehicleType.CAR);
        inv.createInvoice();
        tempVehicle.setInvoiceNumber(inv.invoiceNumber);

        array.addStandard(tempVehicle);
        array.parkVehicle(vehicle);

        System.out.println(tempVehicle.toString());
        System.out.println(vehicle.toString()); 
```

I also have my Arrays, which are on another class. 我也有我的数组,它们在另一个类上。

```
    public MCP(){
       vehicles = new ArrayList<>();
       parkingSpaces = new ArrayList<>();

       parkingSpaces.add(0, null);
       parkingSpaces.add(1, null);

     }


    public void addStandard(Vehicle tempVehicle) {
       vehicles.add(tempVehicle);
     }

     public void parkVehicle(parkingSpace vehicle) {
       parkingSpaces.add(vehicle);
     }
```

This is the way I tried to do it, but I couldn't figure this way out, so I stopped, i'm open to any other ways too. 这是我尝试的方法,但是我无法弄清楚这条路,所以我停了下来,我也对任何其他方式持开放态度。 // public void checkIfEmpty(parkingSpace vehicle){ // if(parkingSpaces.contains(null)){ // parkingSpaces.add(vehicle); // } // else{ // System.out.println("There is no room in this Zone"); // } // }

I am also looking for a better way to populate parking spaces, but that's not the main concern, just something else just incase someone has any ideas 我也在寻找一种更好的填充停车位的方法,但这不是主要问题,只是其他一些事情,以防有人有任何想法

Thanks in Advance. 提前致谢。

Since you are using an array list, when an item is deleted from it, there will be no empty element between two elements in it. 由于您使用的是数组列表,因此从其中删除一项时,其中的两个元素之间将没有空元素。 When you delete an element, the elements shift. 删除元素时,元素会移动。 So in the case of this code, it checks if the number of elements in the array list is smaller than maximum number of cars 因此,对于此代码,它将检查数组列表中的元素数量是否小于最大汽车数量。

int maxSize = 10; //suppose that the maximum number of cars is 10
        public void checkIfEmpty(parkingSpace vehicle){
                if(parkingSpaces.size() < maxSize){
                    parkingSpaces.add(vehicle);
                }
                else{
                    System.out.println("There is no room in this Zone");
                }
            }

Try doing something like this: 尝试做这样的事情:

public void checkIfEmpty(parkingSpace vehicle) {
    boolean addedVehicle = false;
    for (int i = 0; i < parkingSpaces.size(); i++){
        if (parkingSpaces.get(i) == null) {
            parkingSpaces.add(vehicle);
            addedVehicle = true;
            break;
        }
    }

    if (!addedVehicle)
        System.out.println("There is no room in this Zone");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM