Language Tricks to Shorten My Java Code?

Posted by yar on Stack Overflow See other posts from Stack Overflow or by yar
Published on 2010-02-01T12:05:54Z Indexed on 2010/03/22 13:51 UTC
Read the original article Hit count: 514

Filed under:
|
|

I am currently rediscovering Java (working with Ruby a lot recently), and I love the compilation-time checking of everything. It makes refactoring so easy. However, I miss playing fast-and-loose with types to do an each loop. This is my worst code.

Is this as short as it can be? I have a collection called looperTracks, which has instances that implement Looper. I don't want to modify that collection, but I want to iterate through its members PLUS the this (which also implements Looper).

List<Looper> allLoopers = new ArrayList<Looper>(looperTracks.length + 1);
for (LooperTrack track : looperTracks) {
    allLoopers.add(track);
}
allLoopers.add(this);

for (Looper looper : allLoopers) {
    // Finally! I have a looper

I'm particularly concerned about any features that are new to Java from 1.5 on that I may have missed. For this question I am not asking about JRuby nor Groovy, though I know that they would work for this.

Edit: Sorry (too much Ruby!)... looperTracks is of type LooperTrack[] and LooperTrack implements Looper.

© Stack Overflow or respective owner

Related posts about java

Related posts about syntax