No Generic Arrays For You!!

(home)

Much to the disgust of Josh (and the surprise of myself) I am actually somewhat enjoying my recent foray into the murky waters of Java 5 Generics. I’ve spent the last few hours writing a specialised Map that can take multiple values for the same key (yes, I realise that such functionality would technically no longer make it a Map, thus, I called it a Bag :P). Everything went quite smoothly save for one little gotcha: Arrays. I wanted to return an array form a particular method with something like the following:

[The requested file http://tim.littlebluefrog.com/code/26-oct-2005/PropertyBag.java could not be found]

The only problem is that you can’t create generic arrays. Fat lot of good that did me. There is a quite a good explanation of why here for those really interested. The end result is that I implemented these methods using Lists rather than arrays (like I really should have been doing from the start). It’s no big deal, but I can see that generic arrays would be a nice thing to have (curse you type erasure, CURSE YOU!).


2 Responses to “No Generic Arrays For You!!”

  1. CPT Says:

    Shame on you.

  2. tim Says:

    :) Get used to it, by the time 1.7 rolls around it’ll be a given.