我想一个字符串列表转换为双榜如下:为什么将<?>添加到List声明会导致从String列表转换为Double列表时出错?
List<String> myStringList = Arrays.asList("10.0", "10.5", "5.0");
List<?> myDoubleList = new ArrayList();
for(String s : myStringList){
myDoubleList.add(Double.parseDouble(s));
}
当我从列表中移除说明中的<?>
,它的工作原理。但是,当我添加它时会中断。它给了我这个错误:
The method add(capture#6-of ?) in the type List is not applicable for the arguments (double)
这是为什么?
请参阅:http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p它不是一个笨蛋,但最重要的答案解释问题。 –
顺便说一句,声明这样的列表:列表
为什么不是'列表'? –