Please give me a hint as to what is going on here:
List extends Number> a = new ArrayList();
List extends Number> b = new Array
Don't use the ?
wildcard. It means "Some specific type I don't know", and since you don't know the type, you can't add anything to the list. Change your code to use List<Number>
and everything will work.
This is fast becoming the most frequently asked Java question, in a hundred variations...
You have essentially two lists of possibly different types. Because ? extends Number
means a class which extends Number
. So for list a
it can be classA
and for list b
it can be for example classB
. They are not compatible, they can be totally different.
PECS (producer-extends, consumer-super)
The problem is that if you use List<? extends Number>
you could actually do:
List<? extends Number> a = new ArrayList<Integer>();
List<? extends Number> b = new ArrayList<Double>();
a.addAll(b); //ouch, would add Doubles to an Integer list
The compiler can't tell from List<? extends Number>
what the actual type parameter is and thus won't let you do the add operation.
You also shouldn't cast the lists to List<Number>
if you get them as a parameter, since you could actually have a list of Integer objects and add Double objects to it.
In that case you better create a new List<Number>
and add the objects from both lists:
List<Number> c = new ArrayList<Number>(a.size() + b.size());
c.addAll(a);
c.addAll(b);
Edit: in case you create both lists locally, you would not neet the ?
wildcard anyway (since you'd always have List<Number>
).
The thing is:
List<? extends Number> a = new ArrayList<Number>();
List<? extends Number> b = new ArrayList<Number>();
could also be read as:
List<x extends Number> a = new ArrayList<Number>();
List<y extends Number> b = new ArrayList<Number>();
How should the compiler know that x and y are the same?