Error casting HashMap<k,v> to Array<v> in java -


this question has answer here:

i'm having trouble type cas error, , can't find out why keeps poping...

error message : [ljava.lang.object; cannot cast [lnet.masterthought.cucumber.json.stuff;

it pops on line this.stuffs = (stuff[]) slist.toarray();

anyone has idea what's problem ?

public class element {     private stuff[] stuff;      //... getters setters      public function updatestuff(stuff[] newstuff) {         map<string, stuff> stuffmap = new hashmap<string, stuff>();         (stuff s : this.stuffs) {             stuffmap.put(s.getname(), s);         }          (stuff s : prevstuffs) {             if (stuffmap.containskey(s.getname())) {                 stuffmap.get(s.getname()).setvalue(s.getvalue());             }         }         arraylist<stuff> slist = new arraylist<stuff>(stuffmap.values());         this.stuffs = (stuff[]) slist.toarray();     } } 

slist.toarray(); returns object[]. cannot cast stuff[]. can cast array array of supertype, not subtype.

you can stuff[], it's bit awkward.

this.stuffs = new stuff[slist.size()]; slist.toarray(this.stuffs); 

as pointed out in comments, can this:

this.stuffs = slist.toarray(new stuff[0]); 

if argument passed toarray not big enough, new array of same type allocated , returned.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -