I get a compile time error on countList method.
public static void countList( List<? extends Number> list, int count ){
for( int i = 0; i < count; i++ ){
list.set(i, i-1);
}
}
public static void clearList( List<?extends Number> list){
list.clear();
}
It says:
The method set(int, capture#2-of ? extends Number) in the type List is not applicable for the arguments (int, int)
What does this error message mean? Why can’t I set the elements in the list? Why is it OK to clear the list?
Because it’s a list of “Something that extends number, but I don’t know what.” You can’t go just putting integers in there, what if it’s actually a list of doubles?
You can clear the list because for that operation it doesn’t matter which subtype of Number is actually in there. clear is clear is clear.