JDK1.7 source code is as follows:
public static void swap(List<?> list, int i, int j) {
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
What is the meaning of this sentencefinal List l = list? Wouldn't it be the same if you directly manipulate the passed in list?
I am a newbie in self-learning Java. I am a little confused when I see this. I hope the seniors can help me clear up my doubts. Thank you very much!
Try it:
Compilation error:
Because
list
的类型是List<?>
, we don’t know the specific type, so we can only take out an Object from the list, but cannot insert it into the list.So convert it to List.
or replace it with
List<T>
: