But like Herko_ter_Horst pointet out, this may not be the best solution to your problem. Since it is very easy and cheap to calculate the distance between two points, just give your objects a location property, so your "object1.distance(object2)" method just calculates the distance on the fly by substracting the two locations.

The real problem is to find the two objects you need to calculate the exact distance for. In the first place you have to reduce the amount of objects to test, like grouping them in neighbouring clusters. If this is what you are after, Google for "space partitioning trees" like BSP trees, quadtrees and octrees.

You may not need to do any partitioning at all. Testing distance from 1 object to n-1 others is really quick even for hundreds of objects (it scales by (n^2)/2 ). To make it quicker you could even leave out the square root from the distance calculation so you are only computing subtractions. If you store the objects in a list, loop through them like this:

for (int i = 0; i < objects.size(); i++){

for (int j = i+1; j < objects.size(); j++){

// distance test here between i & j

}

}