java


Java removing an iterator that removes another element in the same list


I have an ArrayList called blocks that is iterated through using an iterator. Before i call the .remove() method of the current iterator i must remove another object in the list that is linked to the object being removed. An attempt to do this results in a concurrent modification exception as expected. Do you know how i could work around this? Sample code:
for (Iterator<Block> iterator = Blocks.iterator(); iterator.hasNext();) {
Block block = (Block) iterator.next();
if (block.getX() == x && block.getY() == y) {
block.remove(); //This removes another block from this list but throws the error
iterator.remove();
}
}
If you only need to remove the first match, then the simplest solution would be to iterate through the list to find the first match and save that match in a variable that can be accessed outside of the iterator loop. Then just break out of the loop and perform the cleanup (removal) needed
Block removeMe;
for (Iterator<Block> iterator = Blocks.iterator(); iterator.hasNext();) {
Block block = (Block) iterator.next();
if (block.getX() == x && block.getY() == y) {
removeMe = block;
iterator.remove();
break;
}
}
removeMe.remove();

Related Links

Eclipse E4: E4 Dev Tools Official Download Site. And why Aren't They Included?
Need to make a variable within an action listener public
QueryDSL sorting not working with Spring Data
How to Sort a HashTable [duplicate]
String Pool - String Object - Garbage Collection
How to create framework.odex in android
error in android programming [closed]
How do I create a Stream of regex matches?
JavaFX center an image doesn't work
Calling super method from within an anonymous inner class inside the overridden method
Java SWT ProgressMonitorDialog with UI Thread
Reorder columns in a random order
Load test specific properties when run from integration test or default .yml
Spring Boot any way to define a JTA resource?
Add 2 rows between existing rows
Why is this happening? (Using arrays, increments)

Categories

HOME
ms-access
testng
comparison
relayjs
cvs
jira
win32gui
fsm
installshield
windows-server
fingerprint
opengl-es-2.0
convolution
maude-system
u-sql
hex-editors
fortumo
dynamics-crm-online
serverless-framework
progressive-web-apps
solaris-10
normalizr
xlsxwriter
errorlevel
web-sql
javacv
su
greendao
ioio
google-qpx-express-api
calibre
bower-install
appfabric
serverside-rendering
starteam
optix
email-templates
eclipse-scout
core-plot
vao
wptoolkit
taffy
dandelion
gabor-filter
brightcove
crypt
pillow
flutterwave
account-kit
objective-c-swift-bridge
angular-strap
gridpane
trash
plottable.js
bgp
rvest
bluegiga
pagerank
ipconfig
energy
unity-networking
python-3.2
tmuxinator
content-length
emailrelay
riak-cs
createprocessasuser
dundas
system.reflection
rake-task
gwidgets
nstableviewcell
qpainter
android-imagebutton
phalanger
internet-connection
onactivityresult
google-reader
mesa
reactfx
dealloc
sitemesh
gridcontrol
android-screen-support
specification-pattern
dmoz
sublist
frameset
fireworks
doh
external-accessory
xdomainrequest
file-comparison
gdlib
zend-translate
brewmp
libs
exchange-server-2003
project-hosting
putchar
ubuntu-9.04
multiple-languages
windows-live-messenger
paul-graham

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App