简体   繁体   中英

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.

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.

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. 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.

First of all, I have the function to create the vehicle and store it in an 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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