登录
首页 >  文章 >  java教程

更新ArrayList后如何正确刷新显示

时间:2025-09-19 23:11:30 102浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《更新 ArrayList 元素后如何正确显示》,文章讲解的知识点主要包括,如果你对文章方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

如何在更新 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() 方法

在 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后如何正确刷新显示》文章吧,也可关注golang学习网公众号了解相关技术文章。

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>