boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

如何在更新 ArrayList 中的多个元素后正确显示更新后的元素信息


avatar
作者 2025年9月6日 14

如何在更新 ArrayList 中的多个元素后正确显示更新后的元素信息

本文旨在解决在使用 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() 方法

如何在更新 ArrayList 中的多个元素后正确显示更新后的元素信息

企奶奶

一款专注于企业信息查询的智能大模型,企奶奶查企业,像聊天一样简单。

如何在更新 ArrayList 中的多个元素后正确显示更新后的元素信息24

查看详情 如何在更新 ArrayList 中的多个元素后正确显示更新后的元素信息

在 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 中的多个元素时,显示特定元素信息的常见问题

相关标签:



评论(已关闭)

评论已关闭