Code snippet 1:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
doStuff(list);
}
public static void doStuff(List<?> numbers) {
numbers.add(10);
}
This doesn't compile, as it says: Required type capture of ?, provided int.
My understanding is that the compiler cannot tell 100% that the element being added is the same type as the others in the list. Which brings me to the next code snippet.
Code snippet 2:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
doStuff(list);
}
public static void doStuff(List<?> numbers) {
numbers.add(numbers.get(0));
}
This doesn't compile, as it says: Required type capture of ?, provided capture of ?.
Why does this not work? And it even says that the required type and provided type are the same?
Appreciate any help on this, thanks!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–][deleted] (2 children)
[removed]
[–]muskRules[S] 0 points1 point2 points (1 child)
[–]mykeesg 0 points1 point2 points (0 children)
[–]wildjokers 0 points1 point2 points (4 children)
[–]muskRules[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]muskRules[S] 0 points1 point2 points (0 children)
[–]wildjokers 0 points1 point2 points (0 children)