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

如何正确重写ArrayList的remove()方法?需同步更新客户订单总和

解决方案:重写ArrayList实现增删客户时自动更新订单总和

嘿,我帮你把这个实现补全并优化一下,直接上可用的代码,再给你讲几个容易踩坑的关键点:

第一步:先定义Client类

首先得确保你的Client类有订单金额的属性和对应的获取方法,这样才能计算总和:

public class Client {
    private double orderAmount;

    // 构造方法初始化订单金额
    public Client(double orderAmount) {
        this.orderAmount = orderAmount;
    }

    // 获取订单金额的方法
    public double getOrderAmount() {
        return orderAmount;
    }

    // 可选:如果需要修改客户订单金额,添加setter
    public void setOrderAmount(double orderAmount) {
        this.orderAmount = orderAmount;
    }
}

第二步:完善Path类的实现

这里要注意一个容易漏的点:ArrayList的remove方法有两个重载版本(按对象删除和按索引删除),两个都要重写,不然调用remove(int index)时不会触发总和更新。完整代码如下:

public class Path {
    // 用匿名内部类重写ArrayList的增删方法
    private ArrayList<Client> clients = new ArrayList<Client>() {
        @Override
        public boolean add(Client e) {
            boolean isAdded = super.add(e);
            if (isAdded) {
                // 添加成功后更新总和
                calculateTotalOrder();
            }
            return isAdded;
        }

        // 重写按对象删除的方法
        @Override
        public boolean remove(Object o) {
            boolean isRemoved = super.remove(o);
            if (isRemoved) {
                calculateTotalOrder();
            }
            return isRemoved;
        }

        // 重写按索引删除的方法(很容易漏掉!)
        @Override
        public Client remove(int index) {
            Client removedClient = super.remove(index);
            if (removedClient != null) {
                calculateTotalOrder();
            }
            return removedClient;
        }
    };

    // 存储所有客户的订单总和
    private double totalOrder;

    // 计算并更新订单总和的核心方法
    private void calculateTotalOrder() {
        totalOrder = 0.0;
        // 遍历所有客户累加订单金额
        for (Client client : clients) {
            totalOrder += client.getOrderAmount();
        }
    }

    // 对外提供获取总和的方法
    public double getTotalOrder() {
        return totalOrder;
    }

    // 测试用例
    public static void main(String[] args) {
        Path path = new Path();
        path.clients.add(new Client(150.0));
        path.clients.add(new Client(250.0));
        System.out.println("添加客户后总和:" + path.getTotalOrder()); // 输出400.0

        path.clients.remove(0);
        System.out.println("删除索引0的客户后总和:" + path.getTotalOrder()); // 输出250.0
    }
}

额外优化建议

如果觉得匿名内部类的写法不够清晰,或者后续需要重写更多ArrayList的方法,可以单独写一个继承ArrayList的子类:

public class ClientList extends ArrayList<Client> {
    private Path path;

    public ClientList(Path path) {
        this.path = path;
    }

    @Override
    public boolean add(Client e) {
        boolean isAdded = super.add(e);
        if (isAdded) {
            path.calculateTotalOrder();
        }
        return isAdded;
    }

    @Override
    public boolean remove(Object o) {
        boolean isRemoved = super.remove(o);
        if (isRemoved) {
            path.calculateTotalOrder();
        }
        return isRemoved;
    }

    @Override
    public Client remove(int index) {
        Client removed = super.remove(index);
        if (removed != null) {
            path.calculateTotalOrder();
        }
        return removed;
    }
}

然后在Path类里替换成:

private ClientList clients = new ClientList(this);

另外,如果需要支持修改客户的订单金额时也自动更新总和,可以在Path类里新增一个修改方法:

public boolean updateClientOrder(Client client, double newAmount) {
    int clientIndex = clients.indexOf(client);
    if (clientIndex != -1) {
        clients.get(clientIndex).setOrderAmount(newAmount);
        calculateTotalOrder();
        return true;
    }
    return false;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:08:32