Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

My fix for ConcurrentModificationException

807591Mar 4 2008
I've dealt with this problem a few times and when it popped up in a new
place last week I once again went searching for a solution. I read
through several forum posts all of which offered good suggestions but
no one had a solid answer.

Looking at my code:
Iterator ittr = assets.values().iterator();
while (ittr.hasNext()) {
    addAssetButtonEx(ittr.next());
}
Where assets is defined as:
TreeMap<Integer, Asset> assets

I realized that the method addAssetButtonEx(Asset asset), which is
called in the loop above, creates a good precentage of the user
interface and passes the reference to asset along to every
UI component it creates.

Thinking that the loop may iterate around before the UI creation is completed I came up with this idea:
Asset[] cliAssets = new Asset[assets.size()];
cliAssets = assets.values().toArray(cliAssets);
for (int x = 0; x < cliAssets.length; x++) {
    addAssetButtonEx(cliAssets[x]);
}
This creates an array which points to the internal array of the assets TreeMap. The result is that each iteration of the
loop passes a reference to an array element to addAssetButtonEx()
instead of a temporary object that is gone with the next iteration of
the loop. This fixed my problem.

I hope this helps someone....

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 1 2008
Added on Mar 4 2008
0 comments
101 views