java - 根据用户输入从 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");
}
}
我认为删除车辆的最简单方法是要求用户输入车辆索引号,然后从数组中删除该特定索引号。但我似乎无法取得任何进展,也无法理解从这里去哪里。
解决方案
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");
}
}
推荐阅读
- javascript - 如何在 html 画布中显示来自响应的数据
- java - 第一次“使用”后 JTextField 的操作侦听器失败
- excel - 将多个工作簿的特定工作表复制到另一个工作簿的多个工作表
- python-3.x - 翻译 WTForms 验证错误消息
- reactjs - 将开关/复选框与 Ant Design 表 reatc js 的标题一起放置
- python - 如何删除日期低于特定日期的行
- kubernetes - 蓝绿部署期间正在进行的请求会发生什么情况
- java - Java - 过滤掉最低整数的求和列表
- apache - 运行 docker 文件,但说端口已被占用
- c# - 如何在c#中过滤掉解决方案(.sln)文件中的所有.cs文件