为什么这段代码会出现编译时错误?
public Interface Location {
.......
}
班级代码...
Map<Type, List<? extends Location>> locationsTypeMap = new HashMap<Type, List<? extends Location>>();
/**
Code to add elements to the hashMap.
*/
newLocation = getNewLocation()
while(mapHasElements){
Location.Type key = location.getType();
List<? extends Location> valueList = (List<? extends Location>)locationsTypeMap.get(key); //1
valueList.add(newLocation);/*Compile error*/
}
另一方面,如果我用下面的行替换第 1 步,它可以工作
List<Location> valueList = (List<Location>)locationsTypeMap.get(key);