java - HashSet "cannot convert from element type Object to Integer" when expecting a HashSet of Integer -
in following code expect pn.get(8).get(8)
return array of integers
(in example length of array 1, in program it's longer). instead array of objects
.
can please tell me why , i'm doing wrong.
arraylist<arraylist<hashset<integer>>> pn = new arraylist<arraylist<hashset<integer>>>(); (int x = 0; x < 9; x++) { pn.add(x, new arraylist<hashset<integer>>()); (int y = 0; y < 9; y++) { pn.get(x).add(y, new hashset<integer>()); pn.get(x).get(y).add(new integer(x)); } } (integer i: pn.get(8).get(8).toarray()) { system.out.println(i); }
all need remove toarray()
call in for
statement, returning object[]
instead of integer[]
might have expected. artifact of how java implements generics using type erasure. run time, jvm doesn't know hashset
of type integer
, toarray()
has return array of object[]
.
thankfully, hashset
can iterated directly toarray()
call unnecessary. following works me:
arraylist<arraylist<hashset<integer>>> pn = new arraylist<arraylist<hashset<integer>>>(); (int x = 0; x < 9; x++) { pn.add(x, new arraylist<hashset<integer>>()); (int y = 0; y < 9; y++) { pn.get(x).add(y, new hashset<integer>()); pn.get(x).get(y).add(new integer(x)); } } (integer i: pn.get(8).get(8)) { system.out.println(i); }
Comments
Post a Comment