-
1. Re: ConcurrentModificationException
bfo81 Jul 24, 2006 9:40 AM (in response to bluetrade)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(); }
-
2. Re: ConcurrentModificationException
bluetrade Jul 24, 2006 10:20 AM (in response to bluetrade)Hi
Thanks for your help.
mhm... I was so sure that it is related to some transactional stuff that I didn't even think about that :) - the first example works...
there are actually a couple of access rules that I left out, so it's quite a complex scheme since accounts have default visibility rules and can be hidden from specific users and can be shared with specific users...
Thanks!!!!