I have a list with 13 objects from one class of which a few have the variable "isUsed" set to true and the others to false. How can i remove the objects which have it set to true from the list?


1 2 3 4 5 6 | for (Object obj: nameOfTheList) //for each element in the list { ClassName cn = (Classname) obj; //cast the object, so you can use the 'isUsed' variable if (cn.isUsed) getWorld().removeObject(cn); //without 'getWorld' if it is used in a world subclass //if 'isUsed' isn't public, call the getter method instead } |
1 2 3 4 5 6 7 | List<> copy = nameOfTheList for (Object obj: copy) //for each element in the list { ClassName cn = (Classname) obj; //cast the object, so you can use the 'isUsed' variable if (cn.isUsed) nameOfTheList.remove(cn); //if 'isUsed' isn't public, call the getter method instead } |
1 | List<> copy = nameOfTheList |
1 | List copy = new ArrayList(nameOfTheList); |
1 2 3 4 | for (Iterator i = nameOfTheList.iterator(); i.hasNext(); ) { ClassName cn = (Classname) i.next(); if (cn.isUsed) i.remove(); } |