I try use List instead of List
List<?> list = new ArrayList<Integer>();
...
list.add(1); //compile error
What i do wrong and how cast new value to Integer? Maybe i shoud use List in my project?
List<?>
means a list of some type, but we don't know which type. You can only put objects of the right type into a list - but since you don't know the type, you in effect can't put anything in such a list (other than null
).
There is no way around this, other than declaring your variable as List<Integer>
.
List<Integer> list = new ArrayList<Integer>();
The generic type always has to be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With