How Can You Add Data to Structures of the Type List extends Number>
While working with a list defined as List extends Number> foo3 = new ArrayListThe method add(capture#1-of ? extends Number) in the type List
This error stems from the wildcard declaration of List extends Number> foo3. It indicates that foo3 can hold values from a range of types that inherit from Number. This includes Number, Integer, and Double. Given this flexibility, it becomes difficult to determine what type of object can be safely added to foo3.
For instance:
Ultimately, you cannot add anything to List extends T> because there is no way to guarantee the type of the underlying list. The only certainty is that you can read from it and obtain a value of type T or a subclass of T.
On the other hand, the reverse logic applies to ? super. For example, List super Number> foo3 = new ArrayList
The above is the detailed content of How Can I Add Data to a List. For more information, please follow other related articles on the PHP Chinese website!