I read a book by Java generics and collectionsMaurice Naphthalene.
In the section on capturing wildcards, the author gave an example
public static void reverse(List<?> list) {
rev(list);
}
private static <T> void rev(List<T> list) {
List<T> tmp = new ArrayList<T>(list);
for (int i = 0; i < list.size(); i++) {
list.set(i, tmp.get(list.size()-i-1));
}
}
But I thought that the type List<T>is a subtype List<?>(where List<?>is the shorthand syntax for List<? extends Object>) (is my assumption about the List<T>subtype List<? extends Object>even correct?), So inside the method reverse, how can we pass a variable list(type List<? extends Object>) to a method revwhere the method parameter revhas a type List<T>?
source
share