Tuesday, November 8, 2011

Creating an Array from a Generic class type

Well, I didn't figure this out quick enough but it is as simple as:


@SuppressWarnings("unchecked")
protected final <ArrayType> ArrayType[] getArray(Class<ArrayType> arrayType, int size) {
return (ArrayType[]) Array.newInstance(arrayType, size);
}

Ridicules isn't it...? 
My architecture for a long time was lacking because I was missing this info, and in combination with this post, it makes Generics the most useful feature while designing an application architecture.

No comments:

Post a Comment