简体   繁体   English

为何添加 <?> 从String列表转换为Double列表时,列表声明会导致错误?

[英]Why adding <?> to List declaration causes error when converting from String list to Double list?

I'm trying to convert a String list to Double list as follows: 我正在尝试将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));
}

When I remove the <?> from list description, it works. 当我从列表描述中删除<?> ,它可以工作。 It breaks when I add it however. 但是当我添加它时会中断。 It gives me this error: 它给了我这个错误:

The method add(capture#6-of ?) in the type List is not applicable for the arguments (double) 类型List中的方法add(capture#6-of?)不适用于参数(double)

Why is that ? 这是为什么 ?

The idea behind this is that List<?> represents a generic List of some unknown type. 这背后的想法是List<?>表示某种未知类型的通用List This List<?> could be List<String> , List<Double> , List<File> , or anything really, all of which count as List<?> . 这个List<?>可以是List<String>List<Double>List<File> ,或者其他任何东西,所有这些都算作List<?> This means if you have a variable of type List<?> , you can't just add a Double to it, because myListOfWildcard.add(someDouble) would fail if myListOfWildcard is actually a List<String> , which it could be. 这意味着如果你有一个List<?>类型的变量,你不能只添加一个Double ,因为myListOfWildcard.add(someDouble)如果myListOfWildcard实际上是一个List<String>就会失败。

The wildcards (in particular, List<?> ) aren't as useful for lists that you want to be able to contain anything. 通配符(特别是List<?> )对于您希望能够包含任何内容的列表没有用处。 For those you generally want to use List<Object> . 对于那些您通常想要使用List<Object> A wildcard would be more useful if you want to allow yourself these special cases. 如果您想让自己处于这些特殊情况,那么通配符会更有用。 If you have a method and you want it to be able to accept any type of list whether it be List<String> or List<Double> , this is what List<?> is useful for, because any of these is a List<?> . 如果你有一个方法,并且你希望它能够接受任何类型的列表,无论它是List<String>还是List<Double> ,这就是List<?>对它有用,因为其中任何一个都是List<?> However, you lose the ability to assume anything about the actual generic type parameter of the list, which means you can't just add a Double . 但是,您无法假设列表的实际泛型类型参数,这意味着您不能只添加Double

List<?> is a shortcut for List<? extends Object> List<?>为快捷List<? extends Object> List<? extends Object> . List<? extends Object> Wildcard lists are mostly useful as the arguments for a method; 通配符列表最常用作方法的参数; you usually don't want to create them yourself. 你通常不想自己创造它们。 Collection.containsAll is a good real world example of their use: In particular, Collection.containsAll accepts any Collection as its argument. Collection.containsAll是一个很好的现实使用示例:特别是, Collection.containsAll接受任何Collection作为其参数。 It doesn't have to be a Collection<Object> , but it would if Collection<Object> where listed as its type instead of Collection<?> . 它不必是Collection<Object> ,但如果Collection<Object>列为其类型而不是Collection<?>

Edit: Added paragraphs from comments. 编辑:添加评论段落。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM