Generic collection as a Java method argument
- by Guido
Is there any way to make this work in Java?
public static void change(List<? extends Object> list, int position1, int position2) {
Object obj = list.get(position1);
list.set(position1, list.get(position2));
list.set(position2, obj);
}
The only way I've successfully avoided warnings and errors is this:
public static <T> T change(List<T> list, int position1, int position2) {
T obj = list.get(position1);
list.set(position1, list.get(position2));
list.set(position2, obj);
return obj;
}
but I don't like to be forced to return a value.