Fun With Generics

I was trying to declare a array variable this morning:

Map<String, String>[] mapArray = new Map<String, String>[2];

However, the compiler reports an error with the above statement: “Cannot create a generic array of Map<String,String>”. I had to replace the statement with:

@SuppressWarnings("unchecked") ... Map<String, String>[] mapArray = (Map<String, String>[]) new Map[2];

!?! If one of the goals of generics was to eliminate casts, it certainly failed here.


See also

comments powered by Disqus