So there I was, trying to remove all mappings from a Reaction like this:
for (int i = 0; i < reaction.getMappingCount(); i++) { reaction.removeMapping(i); }and found that only half the mappings were being removed ... can you see why? :)
In fact, this is not some obscure CDK bug, but a logic error on my part. Equivalent code is this:
List list = getListSomehow();using for example a java.util.ArrayList. The problem is that the index (i) is being tested against a changing number (the size). Once half the items have been removed, i is at the half-way point, so on the next pass it stops.
for (int i = 0; i < list.size(); i++) { list.remove(i) }
One way to 'solve' this is to go backwards:
for (int i = list.size(); i > 0; i--) { reaction.removeMapping(i); }but this is slightly less clear than just :
List list = getListSomehow();which is clearer. Of course, even better is the List method removeAll(). It would be nice if Reaction had a similar method...
int size = list.size();
for (int i = 0; i < size; i++) { list.remove(i) }
Comments
@Rajarshi : of course, a nice language (like - say - python) can extend types. Being able to say "public class AtomContainer(List) {" would be great :)