Possible Duplicate:
Java generics and array initialization
How does one instantiate an array of maps in Java?
I know I can do :
Map<String, Object> map = new HashMap<String, Object>();
so I should be able to :
Map<String, Object>[] maps = new HashMap<String, Object>[10];
but this does not work, gives compilation problem.
That’s a quirk of generics in java. You have to declare the array like so:
later you can create each Map personally, example :
This is a consequence of erasure. The array is an array of
HashMaps. The generic type param is not retained. You’ll get a warning about this, but it will compile and you can suppress the warning with the@SuppressWarning("unchecked")annotation.