如何删除ArrayList的元素呢? [英] How to delete elements from ArrayList?

查看:116
本文介绍了如何删除ArrayList的元素呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private static void printInvoice() {

    for (int i = 0; i < customerList.size(); i++) {
        System.out.println(customerList.get(i).getName());
        customerList.get(i).invoiceForCustomer();

    }

在这个名单打印出来,我希望它被删除,有什么建​​议我该怎么办呢?

After this list prints out I want it to be removed, any suggestions how I can do it?

推荐答案

如果你的目标是清除列表中的所有元素(结束了一个空 customerList 后'已经完成打印,并且获取发票),你可以在循环结束后使用ArrayList的clear方法。

If your goal is to clear all elements from the list (end up with an empty customerList after you've finished printing and fetching invoices) you can use ArrayList's clear method after the loop finishes.

private static void printInvoice() {

    for (int i = 0; i < customerList.size(); i++) {
        System.out.println(customerList.get(i).getName());
        customerList.get(i).invoiceForCustomer();

    }

    customerList.clear();

这篇关于如何删除ArrayList的元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆