March 20, 2005

Java improves

Yes, I know this is old news, but I was just fiddling around with some of the recent improvements to Java. Java isn't my favourite language, but I have to use it frequently anyway.

Anyway, I got sick of writing ArrayList<Point2D> points = new ArrayList<Point2D>(); until I noticed that you can do this:
class Points extends ArrayList<Point2D> {}
Points points = new Points();
This might seem like a pointless (no pun intended) way to save a few characters, but now that Points exists, you can start adding methods to it, like this:
class Points extends ArrayList<Point2D> {
double distanceTo(Point2D p1) {
double min = Double.POSITIVE_INFINITY;
for(Point2D p2 : this)
if(p1.distance(p2) < min)
min = p1.distance(p2);
return min;
If, every time you find yourself writing a loop that iterates over a collection of objects, you turn the loop into a method on the collection, you'll soon find that you've got a nice toolkit.

Of course, I'd still rather be using Perl.


Post a Comment

<< Home