繁体   English   中英

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

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

我已经写了一段时间的停车场结构代码,有点卡住了。 到目前为止,我有一个ArrayList,用户可以将Vehicle属性添加到一个停车位的ArrayList中,而该ArrayList带有前面的“ SpaceID”和“ Vehicle”。

到目前为止,我可以做到这样,以便用户添加车辆并将车辆添加到停车位,但是,我已经建立了一个索引为0:且元素(车辆)为空的临时停车位。

从这里开始,我想检查停车位ArrayList是否为“ null”,然后如果找到它,则将车辆替换为null,但是,我不知道如何实现此目的。 我将附加我正在使用的当前代码,或至少附加它的最小版本。

我已经尝试过使用Contains(null)方法,但是我似乎无法使其正常工作,我不确定它是否还能工作,但是从那以后我就从我的代码中删除了它。

首先,我具有创建车辆并将其存储在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()); 
```

我也有我的数组,它们在另一个类上。

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

这是我尝试的方法,但是我无法弄清楚这条路,所以我停了下来,我也对任何其他方式持开放态度。 // public void checkIfEmpty(parkingSpace vehicle){ // if(parkingSpaces.contains(null)){ // parkingSpaces.add(vehicle); // } // else{ // System.out.println("There is no room in this Zone"); // } // }

我也在寻找一种更好的填充停车位的方法,但这不是主要问题,只是其他一些事情,以防有人有任何想法

提前致谢。

由于您使用的是数组列表,因此从其中删除一项时,其中的两个元素之间将没有空元素。 删除元素时,元素会移动。 因此,对于此代码,它将检查数组列表中的元素数量是否小于最大汽车数量。

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

尝试做这样的事情:

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