radders radders - 8 months ago 179
Java Question

Generic array creation error with LinkedHashMap<Integer, String>[]

I've declared a variable as:

LinkedHashMap<Integer, String>[] function_labels;

but when I try and instantiate it with:

function_labels = new LinkedHashMap<Integer, String>[2];

I get a 'generic array creation' error.

I've searched on here, and although there are many posts about this error message,
no-one seems to have offered a solution which actually works, so I'm trying again...

I don't mind what type of collection
is, as long as it works and I can access indexed members of it later. A simple array seems the 'lightest' solution, but there may be others which will work.


Answer Source

you have to cast type to declare like this Generic Array Creation

function_labels = ( LinkedHashMap<Integer, String>[]) new LinkedHashMap<?,?>[2];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download