Hi I’m trying to delete certain elements from a vector. I have a solution working, but to me it’s not elegant or ideal. I’m in MIDP so I don’t have access to Iterator class. Any ideas what’s the best way to implement it?
Current code:
int size = myVector.size();
Object[] copyofObjects = new Window[size];
myVector.copyInto(copyofObjects);
boolean didDelete = false;
for(int i = 0; i < size; i++)
{
Object o = copyofObjects[i];
if(o.shouldBeDeleted())
{
myVector.removeElementAt(myVector.indexOf(o));
continue;
}
}
You can use the way they do it in c++ (std::remove_if).The basic idea is to push all the element you want to delete at the end of the vector , then resize the vector in one shot.
It goes like this : (sorry if my java is a bit rusty)
All that all the element from [0-j[ are the good element , and the element from [j-size-1[, the bad one: