如何删除ArrayList的元素 [英] How to delete elements from arrayList
问题描述
private static void printInvoice() {
for (int i = customerList.size()-1; i >= 0; i--) {
for(int n = taskChecker.size() -1; n >=0 ; i--){
System.out.println(customerList.get(i).getName());
customerList.get(i).invoiceForCustomer();
taskChecker.remove(n);
}
}
这是什么得到打印出来:
This is whats get printed out:
Sven 9900 Task: Rosta knäck Date: 2014-02-11 Numbers of hours: 1
Tor 4950 Task: Rosta bröd Date: 2013-12-12 Numbers of hours: 1
Oden 4950 Task: Rosta bröd Date: 2013-12-12 Numbers of hours: 1
现在我想删除的任务(奥登4950任务:罗斯塔布罗德日期:1:小时2013年12月12日号)我已经打印出来了。我真的糊涂了,我应该怎么做呢?
Now I want to delete the tasks (Oden 4950 Task: Rosta bröd Date: 2013-12-12 Numbers of hours: 1) after I've printed them out. I'm really confused over how I should do this...
推荐答案
您不能从你当前循环通过不使用的Iterator
列表中删除的项目。它构成了一个 ConcurrentModificationException的
。至于你的问题,各国的评论,请使用的Iterator
对象,以允许您在操作上的列表中删除。
You cannot remove items from a list you a currently looping through without using an Iterator
. It constitutes a ConcurrentModificationException
. As the comment to your issue states, use an Iterator
object in order to allow deletion from the list you are operating on.
正如的Iterator
使用一个简单的例子:
Just as a simple example of Iterator
usage:
Iterator<Object> it = listOfObjects.iterator();
while (it.hasNext())
{
Object o = it.next();
it.remove(); <--- removes the last object returned from Iterator.next() from the underlying collection
}
这篇关于如何删除ArrayList的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!