必须从集合中删除元素 [英] got to delete an element from a collection

查看:64
本文介绍了必须从集合中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带值的集合

Object [0] = hashmap(k,v)

Object [1] = hashmap(k,v)

Object [2] = hashmap(k,v)

Object [3] = hashmap(k,v)

Object [4] = hashmap( k,v)


i terated这个集合和删除的对象[1]。然后,当我再次迭代时,我得到以下异常。

java.util.ConcurrentModificationException


i想要删除元素直到Object [3]。

我该怎么做

请帮助

Ive got a collection with values
Object[0]= hashmap(k,v)
Object[1]= hashmap(k,v)
Object[2]= hashmap(k,v)
Object[3]= hashmap(k,v)
Object[4]= hashmap(k,v)

i terated this collection and deleted object[1]. then when i iterate again i get the following exception.
java.util.ConcurrentModificationException

i want to delete elements till Object[3].
how do i do this
please help

推荐答案


我有具有值的集合

Object [0] = hashmap(k,v)

Object [1] = hashmap(k,v)

Object [2] = hashmap(k,v)

Object [3] = hashmap(k,v)

Object [4] = hashmap(k,v)


i terated这个集合并删除了对象[1]。然后,当我再次迭代时,我得到以下异常。

java.util.ConcurrentModificationException


i想要删除元素直到Object [3]。

我该怎么做

请帮助
Ive got a collection with values
Object[0]= hashmap(k,v)
Object[1]= hashmap(k,v)
Object[2]= hashmap(k,v)
Object[3]= hashmap(k,v)
Object[4]= hashmap(k,v)

i terated this collection and deleted object[1]. then when i iterate again i get the following exception.
java.util.ConcurrentModificationException

i want to delete elements till Object[3].
how do i do this
please help



你是如何删除的?发布删除部分的代码

How did you delete? Post your code for the deleting part



你是如何删除的?发布删除部分的代码
How did you delete? Post your code for the deleting part



for(Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();){

Object object =(Object)iter.next();

HashMap campusModel =(HashMap)对象;


voucherTransactionCols.remove(campusModel);

}


现在我可以删除元素但是当我删除Object [3]时,元素Object [4]移动到第三个位置。所以我不能删除集合中的最后一个元素。


for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();) {
Object object = (Object) iter.next();
HashMap campusModel = (HashMap) object;

voucherTransactionCols.remove(campusModel);
}

Now i can delete the elements but when i delete Object[3] , the element Object[4] moves to the third position . so i cant delete the last elenment in the collection.


仔细查看此代码.....


集合c =这里的一些代码;


标签:

for(int i = 0; i< c.size(); i ++)

if(condition )

{

c.remove(c.get(i));

继续标签;

}


试试这段代码..........

一切顺利
see this code carefully .....

Collection c = some code here;

Label:
for(int i=0;i<c.size();i++)
if(condition)
{
c.remove(c.get(i));
continue Label;
}

try this code ..........
all the best


这篇关于必须从集合中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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