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

how to insert multiple records from json string into google datastore in java
Java Spring security - 'Error 404'
Markers not shown on styled MapView on Mapxbox Android
Open new transaction when already inside ejb
Unable to run Java code with Apache Commons
Java Web Application Structure Set Welcome Page
IllegalStateException: Cannot convert value of type A to required type : no matching editors or conversion strategy found
Android FacebookActivity: Cannot call LoginFragment with a null calling package
HashMap returning false always for containsValue(),
How to make Maven 3.2.5 understand Java 6 project on a machine using Java 7
maven download stuck at invalid path
Java sound doesn't work on Windows, but work on OS X
Is there any way in Java to take image width and height without transfer or download?
(SOLVED) How do I acces attributes from an object, inside a List, in the same class?
org.apache.http.client-4.3.6: java.lang.NoClassDefFoundError: org.apache.http.impl.conn.PoolingHttpClientConnectionManager
Pass form data from Spring Controller to an external endpoint

Categories

HOME
ibm-bluemix
appx
bpmn
jsp-tags
maven-3
jxls
cross-browser
enterprise-library-5
flyway4
u-sql
google-cloud-ml
google-cloud-spanner
leiningen
windows-7-x64
http-status-code-504
oxyplot
samoa
lucene.net
visual-composer
dbext
one-hot-encoding
css-animations
cultureinfo
sparse-matrix
facet
typo3-6.2.x
procdump
librato
calibre
pim
y86
google-api-nodejs-client
swisscomdev
x11-forwarding
hot-module-replacement
graphenedb
pdf-reactor
glew
nand2tetris
eclipse-gef
xml-documentation
errordocument
rails-routing
jquery-validate
boost-multi-index
tizen-native-app
font-size
mcafee
dynamics-sl
minimization
blacklist
migradoc
xcb
setuptools
httplistener
radtreelist
transmitfile
ubuntu-10.04
qgraphicsview
hexdump
php-parse-error
goose
wso2cloud
django-unittest
event-bubbling
ami
remobjects
sonarqube5.1.2
pretty-print
facebook-graph-api-v2.4
xc16
android-nested-fragment
javafx-webengine
onactivityresult
zend-route
p4java
expected-exception
system.net.webexception
braille
picturefill
oam
gridfs
yui-compressor
android-screen-support
coderush
gdata-api
first-responder
onsubmit
parametric-equations
online-compilation
qtkit
bubble-chart
semantic-diff
work-stealing
servicehost
table-footer
datareader
fluent-interface
subviews
gamequery
xsdobjectgen
libs
uimenucontroller
simpletest
mdac
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