2

I've an ArrayList in Java of my class 'Bomb'.

This class has a method 'isExploded', this method will return true if the bomb has been exploded, else false.

Now I'm trying to iterate through this arraylist, call this method isExploded and remove the element from the list if it returns true.

I know how to iterate:

    for (Iterator i = bombGrid.listIterator(); i.hasNext();) {
    if () {         
        i.remove();
}

But I've no idea how to access the method isExploded of the Bomb class itself via the iterator. Does anyone know the answer to this?

Sincerely,

Luxo

3 Answers 3

5

You'll need to get the Bomb using next :

for (Iterator i = bombGrid.listIterator(); i.hasNext();) {
   Bomb bomb = (Bomb) i.next(); 
   if (bomb.isExploded()) i.remove();
}

Or if you can get an Iterator<Bomb> from your bombGrid (is it an ArrayList<Bomb> ?):

Iterator<Bomb> i = bombGrid.listIterator();
while (i.hasNext()) {
   Bomb bomb = i.next(); 
   if (bomb.isExploded()) i.remove();
}

This supposes your iterator supports remove, which is the case for example by the one given by an ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

0

If you use Java 5, use generics:

List<Bomb> bombGrid = ...;
for (Iterator<Bomb> i = bombGrid.iterator(); i.hasNext();) {
  if (i.next().isExploded()) {         
    i.remove();
  }
}

Comments

-2

No, you cannot remove inside an Iterator for ArrayList while iterating on it. Here's Javadoc extract :

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

9 Comments

If the list is modified using the iterator, it's fine.
Look at the implementation of the class Itr : it handles the case of the removal of the current element.
sorry, but even if it work, I maintain it's not a good practice as it is stated in javadoc : Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs. And I also see a potential memory leak
No. Clearly no. Java designers have solved an important problem here. Losing the ability to remove while iterating just because you want to use an interface, and thus losing performances and making your code more complex just for that, sounds, pardon me, totally nuts...
@user1898956: in the unlikely case someone uses a list that doesn't support removal, then your unit test for the method should catch it. Keep using interfaces, and write unit tests to verify the code works as it should, and you'll have clean, logical and efficient code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.