When You Go Rookie and Forget that Arrays Have Indices
You get really lousy code - Rube Goldberg code, that's what 😊
You have to find a figure-of-merit that is the best from a list and then get the object with that FOM. You can, but I forgot that, in an ArrayList, the index is all you need 😊
And, having forgotten, I ended up with :
for( int i=0; i<howMany; i++){
double leastDist = 30000000; // meters
double distance;
for( QuakeEntry qe : copy ){
distance = current.distanceTo( qe.getLocation() );
if( distance < leastDist ){
leastDist = distance;
}
}
for( QuakeEntry qe : quakeData ){
distance = current.distanceTo( qe.getLocation() );
if( Math.abs( distance - leastDist) < 1 ){
copy.remove(qe);
ret.add(qe);
}
}
}
When all I really needed was :
for( int i=0; i<howMany; i++){
int minIndex = 0; // start with the first
double distance;
for( int k=1; k<copy.size(); k++ ){
distance = current.distanceTo( copy.get(k).getLocation() );
if( distance < current.distanceTo(copy.get(minIndex).getLocation() ) ){
minIndex = k;
}
}
ret.add(copy.get(minIndex));
copy.remove(minIndex);
}
Comments
Post a Comment