views:

131

answers:

3

This is what I tried to do, but it gives me a warning:

        HashMap<String, String>[] responseArray = new HashMap[games.size()];

"Type safety: The expression of type HashMap[] needs unchecked conversion to conform to HashMap[]"

+2  A: 

What gives? It works. Just ignore it:

@SuppressWarnings("unchecked")

No, you cannot parameterize it. I'd however rather use a List<Map<K, V>> instead.

List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();

To learn more about collections and maps, have a look at this tutorial.

BalusC
Thanks. I got it working using a list of maps. I don't like ignoring warnings. In my experience if you're getting a warning, ur doin' it rong. The only thing I don't understand is why I declare it as a type Map, but when I actually instantiate it I have to use HashMap?Map<String, String> responseMap;responseMap = new HashMap<String, String>();
Joren
It's an interface. A blueprint. A contract. Follow the link :)
BalusC
A: 

You can't have an array of a generic type. Use List instead.

Tom
Actually, you can *have* an array of a generic type, you just can't *create* one directly. (I don't presume to understand the rationale behind this, but that's what the spec says)
meriton
+2  A: 

The Java Language Specification, section 15.10, states:

An array creation expression creates an object that is a new array whose elements are of the type specified by the PrimitiveType or ClassOrInterfaceType. It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type (§4.7).

and

The rules above imply that the element type in an array creation expression cannot be a parameterized type, other than an unbounded wildcard.

The closest you can do is use an unchecked cast, either from the raw type, as you have done, or from an unbounded wildcard:

 HashMap<String, String>[] responseArray = (Map<String, String>[]) new HashMap<?,?>[games.size()];

Your version is clearly better :-)

meriton