My new work and my current temporary home are on the same street - only 13 km apart. The street changes names a couple times as it traverses severals boroughs of Berlin, but it's really a straight ride, mostly with good bike lanes on the street or sidewalk. Despite being totally inside the city, the density of traffic lights on this road is rather low and so I travel much faster on my bike than I would in Toronto. It's also totally flat!
Yesterday, I even found a nicer route that uses quieter streets:
View Larger Map
Moving to Berlin is a real improvement in terms of quality of life! And the route to the sailing club is even better, half of it leading right through the Grunewald forest.
28 June 2010
1 June 2010
literal arrays and lists in Java
In Python and other modern languages, you can say things like:
For people who need a lot of literal arrays, this can be a bother, especially when you need small arrays on the inside of expressions. Fortunately, Java's new “variable length argument lists” can help here, because they transform any number of arguments into just.... an array. So this little helper function does the job:
Now I can happily write:
Java's standard library even provides a function to create literal lists. Ironically this function is placed in class Arrays although it neither eats nor produces an array.
The nice thing about this factory method is that it provides a list which is backed by an array, so you get a list of fixed size – that saves a little bit of memory and can be useful in some situations. Unfortunately, the documentation says nothing about what happens when you call size-changing methods (like add()) on the resulting list. It's a typical omission of the Java documentation. One has to figure out all the details by oneself. Unfortunately, many Java programmers don't even know that there are such details and then they are surprised when it hits them.
I just did a quick history research. It seems that the helpful function asList(...) exists in Java at least since 1998 (Java 1.2, with the Collections framework). Given that Java really became famous around and after this time, it is really deplorable, that most Java tutorials introduce lists like this:
When they instead could write:
Admittedly, the “for ( i : ls )” syntax and variables length argument lists have only been deployed with Java 1.5 in 2004. Curiously, the simplified for loop has become widespread already, while varargs and list literals haven't.
Update: I was about to edit my program to make use of the simplified syntax when I discovered, that Java has anonymous array literals after all. I used Ecplise's “inline local variable” refactoring to get rid of the array dummy and what it produced was:
Curious again: the feature is there (probably even since an early release), but people don't know about it!
In Java, there is no direct syntax for array or list literals. The only thing that Java has are “Array Initializers” that can be used right where an array is declared. Thus the above Python code becomes:for x in ['cat', 'window', 'defenestrate']: print x, len(x)
String[] xs = {"cat", "window", "defenestrate"}; for (String x : xs) System.out.println(x);
For people who need a lot of literal arrays, this can be a bother, especially when you need small arrays on the inside of expressions. Fortunately, Java's new “variable length argument lists” can help here, because they transform any number of arguments into just.... an array. So this little helper function does the job:
staticT[] array(T... elems) { return elems; }
Now I can happily write:
for (String x : array("cat", "window", "defenestrate")) System.out.println(x);
Java's standard library even provides a function to create literal lists. Ironically this function is placed in class Arrays although it neither eats nor produces an array.
public staticList asList(T... a)
The nice thing about this factory method is that it provides a list which is backed by an array, so you get a list of fixed size – that saves a little bit of memory and can be useful in some situations. Unfortunately, the documentation says nothing about what happens when you call size-changing methods (like add()) on the resulting list. It's a typical omission of the Java documentation. One has to figure out all the details by oneself. Unfortunately, many Java programmers don't even know that there are such details and then they are surprised when it hits them.
I just did a quick history research. It seems that the helpful function asList(...) exists in Java at least since 1998 (Java 1.2, with the Collections framework). Given that Java really became famous around and after this time, it is really deplorable, that most Java tutorials introduce lists like this:
Listls = new ArrayList (); ls.add(1); ls.add(2); for ( Integer i : ls ) // do stuff
When they instead could write:
for ( Integer i : Arrays.asList(1, 2) ) // do stuff
Admittedly, the “for ( i : ls )” syntax and variables length argument lists have only been deployed with Java 1.5 in 2004. Curiously, the simplified for loop has become widespread already, while varargs and list literals haven't.
Update: I was about to edit my program to make use of the simplified syntax when I discovered, that Java has anonymous array literals after all. I used Ecplise's “inline local variable” refactoring to get rid of the array dummy and what it produced was:
return new Object[] {1, 2, 3};
Curious again: the feature is there (probably even since an early release), but people don't know about it!
Subscribe to:
Posts (Atom)