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);
}
}
这是什么打印出来的:
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
现在我想在打印出来之后删除任务(Oden 4950任务:Rostabröd日期:2013-12-12小时数:1)。我真的很困惑我应该怎么做......
答案 0 :(得分:3)
如果不使用Iterator
,则无法从当前循环播放的列表中删除项目。它构成ConcurrentModificationException
。正如对您的问题的评论所述,使用Iterator
对象以允许从您正在操作的列表中删除。
正如Iterator
用法的简单示例:
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
}