21

I would like to remove an object from an ArrayList when I'm done with it, but I can't find way to do it. Trying to remove it like in the sample code below doesn't want to work. How could I get to the iterator of current px object in this loop to remove it?

for( Pixel px : pixel){ 
[...]
  if(px.y > gHeigh){
     pixel.remove(pixel.indexOf(px)); // here is the thing
     pixel.remove(px); //doesn't work either
  }
}
Null
  • 1,950
  • 9
  • 30
  • 33
JakobekS
  • 257
  • 1
  • 3
  • 10
  • 1
    possible duplicate of [Calling remove in foreach loop in Java](http://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java) – DNA Mar 13 '12 at 20:21
  • possible duplicate of [Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – omerhakanbilici Oct 31 '16 at 14:16

6 Answers6

67

You can't, within the enhanced for loop. You have to use the "long-hand" approach:

for (Iterator<Pixel> iterator = pixels.iterator(); iterator.hasNext(); ) {
  Pixel px = iterator.next();
  if(px.y > gHeigh){
    iterator.remove();
  }
}

Of course, not all iterators support removal, but you should be fine with ArrayList.

An alternative is to build an additional collection of "pixels to remove" then call removeAll on the list at the end.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
24

Using and lamdba expressions, the method removeIf has been introduced for collections.

Removes all of the elements of this collection that satisfy the given predicate.

So it will only take one line :

pixels.removeIf(px -> px.y > gHeigh);
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 1
    by the way `removeIf` uses `Iterator` and `while` loop. You can see it at java 8 `java.util.Collection.java` – omerhakanbilici Oct 31 '16 at 14:16
  • 1
    @omerhakanbilici This is only the default implementation. You can see it has been optimized for `ArrayList`s for instance. – Alexis C. Oct 31 '16 at 15:14
2

you need to create and access the iterator explicitly

Iterator<Pixel> it = pixel.iterator();
while(it.hasNext()){
Pixel.px = it.next();
//...
it.remove();
}
Bumptious Q Bangwhistle
  • 4,689
  • 2
  • 34
  • 43
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
2

You cannot modify a Collection while someone is iterating over it, even if that someone were you. Use normal for cycle:

for(int i = 0; i < pixel.size(); i++){
    if(pixel.get(i).y > gHeigh){
        pixel.remove(i);
        i--;
    }
}
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
1

use a regular for loop, the enhanced for loop maintains an iterator, and doesn't allow for removal of objects, or use the iterator explicitly

Edit: see answer of the this question Calling remove in foreach loop in Java

Community
  • 1
  • 1
Alex
  • 10,470
  • 8
  • 40
  • 62
0

If Pixel is your own custom object then you need to implement the equals and hashcode method for your Pixel object. The indexOf method also finds the index using equals method. Try implementing that and check it out.

raddykrish
  • 1,866
  • 13
  • 15