
本文旨在解决在使用 ArrayList 存储车辆信息时,更新车辆信息后如何正确显示更新后的车辆详情的问题。重点在于修改 displayCurrentVehicleEntry() 方法,使其能够根据索引显示 ArrayList 中特定位置的车辆信息,从而解决更新多个元素后只显示最后一个元素的问题。
原代码中,displayCurrentVehicleEntry() 方法总是显示 listOfVehicles 中最后一个元素的信息,这是因为该方法内部始终使用 listOfVehicles.size() - 1 作为索引来获取车辆信息。因此,在 updateVehicle() 方法中调用 displayCurrentVehicleEntry() 时,无论更新了哪个车辆的信息,总是会显示最后一个车辆的信息。
为了解决这个问题,需要修改 displayCurrentVehicleEntry() 方法,使其能够接收一个索引参数,并根据该索引显示 ArrayList 中特定位置的车辆信息。
1. 修改 displayCurrentVehicleEntry() 方法
修改后的 displayCurrentVehicleEntry() 方法如下:
public void displayCurrentVehicleEntry(int index) {
try {
AutoInv vehicle = listOfVehicles.get(index);
System.out.println("Make: " + vehicle.getMake().toUpperCase());
System.out.println("Model: " + vehicle.getModel().toUpperCase());
System.out.println("Color: " + vehicle.getColor().toUpperCase());
System.out.println("Year: " + vehicle.getYear());
System.out.println("Mileage: " + vehicle.getMileage());
System.out.println("");
} catch (Exception e) {
System.out.println("Failure");
System.out.println(e.getMessage());
e.printStackTrace();
}
}2. 修改 addVehicle() 方法
在 addVehicle() 方法中,调用 displayCurrentVehicleEntry() 时,传入新添加车辆的索引:
public void addVehicle(AutoInv vehicle) throws Exception{
try {
if (listOfVehicles.add(vehicle)) {
System.out.println("\nFollowing vehicle added successfully:\n");
displayCurrentVehicleEntry(listOfVehicles.size() - 1);
}
else {
throw new Exception("\nFailed to add vehicle.");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}3. 修改 updateVehicle() 方法
在 updateVehicle() 方法中,调用 displayCurrentVehicleEntry() 时,传入被更新车辆的索引:
public void updateVehicle(String makeCurrent, String modelCurrent, String colorCurrent, int yearCurrent, int mileageCurrent,
String makeUpdated, String modelUpdated, String colorUpdated, int yearUpdated, int mileageUpdated) {
try {
boolean found = false;
for (int i = 0; i < listOfVehicles.size(); i++) {
AutoInv vehicle = listOfVehicles.get(i);
if (vehicle.getMake().equalsIgnoreCase(makeCurrent)
&& vehicle.getModel().equalsIgnoreCase(modelCurrent)
&& vehicle.getColor().equalsIgnoreCase(colorCurrent)
&& vehicle.getYear() == yearCurrent
&& vehicle.getMileage() == mileageCurrent) {
vehicle.setMake(makeUpdated);
vehicle.setModel(modelUpdated);
vehicle.setColor(colorUpdated);
vehicle.setYear(yearUpdated);
vehicle.setMileage(mileageUpdated);
System.out.println("\nVehicle updated successfully!\n");
displayCurrentVehicleEntry(i);
found = true;
}
}
if (!found) {
System.out.println("\nVehicle not found in inventory!");
}
} catch (Exception e) {
System.out.println("Failure");
System.out.println(e.getMessage());
e.printStackTrace();
}
}通过修改 displayCurrentVehicleEntry() 方法,使其能够接收索引参数,并在 addVehicle() 和 updateVehicle() 方法中传入正确的索引,可以确保在添加或更新车辆信息后,能够正确显示对应车辆的详细信息。这个简单的修改解决了在处理 ArrayList 中的多个元素时,显示特定元素信息的常见问题。
以上就是如何在更新 ArrayList 中的多个元素后正确显示更新后的元素信息的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号