[jboss-user] [JBoss Seam] - Re: ConcurrentModificationException

bfo81 do-not-reply at jboss.com
Mon Jul 24 09:40:53 EDT 2006


You cannot change the length of a Collection (by adding or removing items) while iterating through it.

You can
- make a copy of the account collections, delete accounts from that copy during iteration, clear the account collection and insert all elements of the copy into the account collection *uff*
- insert all accounts to delete into a toDelete collection during iteration, and then iterate over the toDelete collection and delete items from the account collection.

Confused? Here's an example for the second way: 

public void filter(...) {
  |   Collection<Accounts> toDelete = new ArrayList<Accounts>();
  |     for (Account a: accounts)
  |         if(accounts.notAccessibleBy(user)
  |             toDelete.add(a);
  |         else if (accounts.accessibleBy(user)
  |             continue;
  |         else
  |             toDelete.add(a);
  | 
  | for (Account a: toDelete)
  |     accounts.remove(a);
  | }
btw: The checks you make (notAccessiblyBy, accessibleBy) seem to be a bit weird ;). It looks like you want to keep ALL accounts in the collection or delete ALL of them. Wouldn't it work like this?
public void filter(...) {
  |     if (accounts.notAccessibleBy(user))
  |         accounts.clear();
  | }
 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3960375#3960375

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3960375



More information about the jboss-user mailing list