You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArrayList多元素场景下如何正确显示更新后的车辆库存信息?

解决车辆库存更新后无法正确显示更新车辆详情的问题

当前车辆库存更新程序中,updateVehicle()方法成功更新匹配到的车辆信息后,调用displayCurrentVehicleEntry()却总是显示列表最后一辆车的信息,而非刚更新的目标车辆。原因是displayCurrentVehicleEntry()硬编码获取列表最后一个元素,仅适配addVehicle()场景,无法满足更新操作的需求。

解决方案

核心思路是让显示方法支持传入指定的AutoInv对象,而非固定取列表最后一项,实现不同场景下的精准展示。

1. 重载显示方法,支持传入指定车辆

修改displayCurrentVehicleEntry(),新增重载版本接收AutoInv参数,同时保留原方法适配新增场景:

// 重载方法:接收指定车辆进行展示
public void displayCurrentVehicleEntry(AutoInv vehicle) {
    try {
        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();
    }   
}

// 原方法:适配新增场景,复用重载方法逻辑
public void displayCurrentVehicleEntry() {
    if (listOfVehicles.isEmpty()) {
        System.out.println("Inventory is empty!");
        return;
    }
    AutoInv vehicle = listOfVehicles.get(listOfVehicles.size() - 1);
    displayCurrentVehicleEntry(vehicle);
}

2. 修改更新方法,传入刚更新的车辆对象

在updateVehicle()中调用重载后的显示方法,直接传入已更新的车辆对象:

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(vehicle); // 传入更新后的目标车辆
                found = true;
                break; // 找到匹配车辆后跳出循环,避免重复处理(若允许重复车辆可移除该语句)
            }
        }
        if (!found) {
            System.out.println("\nVehicle not found in inventory!");
        }
    } catch (Exception e) {
        System.out.println("Failure");
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

3. 新增方法无需修改

addVehicle()原调用逻辑保持不变,原displayCurrentVehicleEntry()会自动取列表最后一个元素并调用重载方法完成展示。

优化优势

  • 代码复用:重载方法避免了重复的显示逻辑,后续维护更便捷
  • 场景适配:同时满足新增、更新两种场景的精准展示需求
  • 健壮性提升:原方法增加空列表判断,避免索引越界异常

内容的提问来源于stack exchange,提问作者cleatus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:51:41