java - How to add an integer element to ArrayList of wildcards in Generics? -
i have arraylist generic wildcards type, taking number extends. trying add integer values arraylist.
buts it's giving me error saying that
arraylist<? extends number> numberlist = new arraylist<number>(); numberlist = new arraylist<integer>(); numberlist.add(100);
the method add(int, capture#2-of ?) in type
arraylist<capture#2-of ?>
not applicable arguments (int).
i have tried way also, giving me same error
arraylist<?> numberlist = new arraylist<number>(); numberlist = new arraylist<integer>(); numberlist.add(100);
the error :
the method add(int, capture#2-of ?) in type
arraylist<capture#2-of ?>
not applicable arguments (int)
you can't. ? extends
part tells compiler: of type, don't know, extends number.
so compiler can't guarantee type of whatever want add compatible unknown type. therefor can't add such collection.
Comments
Post a Comment