首页 > 解决方案 > 根据用户输入从 ArrayList 中删除

问题描述

为基础编程课程建立一个非常简单的车辆库存。它需要一个添加新车辆方法、列出车辆信息方法、删除车辆方法和更新车辆属性方法。

我用 set 和 get 方法构建了一个汽车类:

    private String make;
    private String model;
    private String color;
    private int year;
    private int mileage;
    private int index;


    public Automobile(String make, String model, String color, int year,
                      int mileage, int index) {
        this.make = make;
        this.model = model;
        this.color = color;
        this.year = year;
        this.mileage = mileage;
        this.index = index;
    }



    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMileage() {
        return mileage;
    }

    public void setMileage(int mileage) {
        this.mileage = mileage;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

}

在我的 Vehicle 类中,我现在有一个看起来可以工作的 addVehicle 方法。

class Vehicle {
    static ArrayList<Automobile> vehicleList = new ArrayList<>();

    public static void addVehicle() {
        Scanner input = new Scanner(System.in);
        System.out.println("## Add Vehicle to Inventory ##");
        System.out.print("Enter Vehicle make: ");
        String make = input.nextLine();
        System.out.print("Enter Vehicle model: ");
        String model = input.nextLine();
        System.out.print("Enter Vehicle color: ");
        String color = input.nextLine();
        System.out.print("Enter Vehicle year: ");
        int year = input.nextInt();
        System.out.print("Enter Vehicle mileage: ");
        int mileage = input.nextInt();
        System.out.print("Enter the Vehicle Index Number: ");
        int index = input.nextInt();
        
        Automobile newCar = new Automobile(make, model, color, year, mileage, index);
        
        vehicleList.add(newCar);
        
        System.out.println("Vehicle Added to Inventory Successfully");

    }

但是,我坚持使用我正在尝试构建的 removeVehicle 方法:

    public void removeVehicle() {
        Scanner input = new Scanner(System.in);
        System.out.println("## Remove Vehicle from Inventory ##");
        System.out.print("Enter the Vehicle Index Number: ");
        int indexRemove = input.nextInt();

        if(    ){
            System.out.println("Vehicle Removed Successfully");
        }
        else{
            System.out.println("No Such Vehicle Exists");
        }

    }

我认为删除车辆的最简单方法是要求用户输入车辆索引号,然后从数组中删除该特定索引号。但我似乎无法取得任何进展,也无法理解从这里去哪里。

标签: javaarraylist

解决方案


public void removeVehicle() {
    Scanner input = new Scanner(System.in);
    System.out.println("## Remove Vehicle from Inventory ##");
    System.out.print("Enter the Vehicle Index Number: ");
    int indexRemove = input.nextInt();

    int oldSize = vehicleList.size();

    for (Automobile vehicle : vehicleList) {
        if (auto.getIndex() == indexRemove) {
            vehicleList.remove(vehicle);
            System.out.println("Vehicle Removed Successfully");
            break;
        }
    }

    if (vehicleList.size() == oldSize) {
        System.out.println("No Such Vehicle Exists");
    }
}

推荐阅读