没有类型变量的实例存在,使得“Object”符合“Number”。

huangapple go评论74阅读模式
英文:

No instance of type variables exists so that Object confirms to Number

问题

为什么下面的代码会抛出这个错误:没有类型变量的实例,使得 Object 能够确认为 Number?

List<Object> source = Arrays.asList(1, 2, 3);
List<Number> dest = Arrays.asList(4.1, 5, 6, 9.1);
Collections.copy(dest, source);
英文:

Why is the below code throwing this error : No instance of type variables exists so that Object confirms to Number?

List&lt;Object&gt; source = Arrays.asList(1,2,3);
List&lt;Number&gt; dest = Arrays.asList(4.1,5,6,9.1);
Collections.copy(dest, source);

答案1

得分: 3

你可以将源List进行转换以使你的代码工作:

Collections.copy(dest, (List<Number>) (List) source);

请注意,如果source包含非数字元素,这段代码将抛出一个晦涩的异常(ArrayStoreException)。也许你应该重新考虑将数字存储在一个Object列表中。

由于Collections.copy的签名,你无法将一个Object列表复制到一个Number列表中:

public static <T> void copy(List<? super T> dest, List<? extends T> src)

这两个约束基本上保证了你不能直接将一个超类型的列表复制到一个子类型的列表中。

在JDK 8中,你会得到一个稍微更有帮助的错误信息:

Main.java:14: error: method copy in class Collections cannot be applied to given types;
Collections.copy(dest, source);
           ^
  required: List<? super T>,List<? extends T>
  found: List<Number>,List<Object>
  reason: inference variable T has incompatible bounds
    lower bounds: Number,Object
    upper bounds: Object
  where T is a type-variable:
    T extends Object declared in method <T>copy(List<? super T>,List<? extends T>)
英文:

You can cast the source List to make your code work:

Collections.copy(dest, (List&lt;Number&gt;) (List) source);

Please note that this code will throw an obscure exception (ArrayStoreException) if source contains non-number elements. Perhaps you should reconsider storing numbers in a list of Object.

You can't copy a list of Object to a list of Number because of the signature of Collections.copy:

public static &lt;T&gt; void copy(List&lt;? super T&gt; dest, List&lt;? extends T&gt; src)

These two constraints basically guarantee that you can't directly copy a list of supertypes to a list of subtypes.

On JDK 8 you get a slightly more helpful error message:

Main.java:14: error: method copy in class Collections cannot be applied to given types;
Collections.copy(dest, source);
           ^
  required: List&lt;? super T&gt;,List&lt;? extends T&gt;
  found: List&lt;Number&gt;,List&lt;Object&gt;
  reason: inference variable T has incompatible bounds
    lower bounds: Number,Object
    lower bounds: Object
  where T is a type-variable:
    T extends Object declared in method &lt;T&gt;copy(List&lt;? super T&gt;,List&lt;? extends T&gt;)

答案2

得分: 1

如果你查看Collections.copy函数:

public static <T> void copy(List<? super T> dest, List<? extends T> src) {}

你会发现两个参数都应该是List类型,而且dest应该在继承层级上高于src。在你的例子中 - Number <- Object,因此会出现编译时异常。

如果你这样做:

Collections.copy(source, dest);

你将不会遇到这种错误。

英文:

If you look at Collections.copy:

public static &lt;T&gt; void copy(List&lt;? super T&gt; dest, List&lt;? extends T&gt; src) {}

you find out that both arguments should be List and dest should be higher in the hierarhy than src. In your example - Number &lt;- Object so you have ComplieTimeException.

If you do like this:

Collections.copy(source, dest);

you're not going to have such error.

答案3

得分: 1

因为目标数组中的所有元素都必须是 Number 类型,但是源数组中的对象不一定是这种类型,因为它们是 Object 类型,可以是任何类型。

请参考:

List<Object> source = Arrays.asList(1, 2, 3, "在这里允许字符串");
List<Number> dest = Arrays.asList(4.1, 5, 6, 9.1);
Collections.copy(dest, source);
英文:

It's because all elements in dest array must be of type Number but there's no guratantee that objects in source array are of this type cause they are of type Object and can be whatever.

please see:

    List&lt;Object&gt; source = Arrays.asList(1,2,3,&quot;string allowed here&quot;);
    List&lt;Number&gt; dest = Arrays.asList(4.1,5,6,9.1);
    Collections.copy(dest, source);

huangapple
  • 本文由 发表于 2020年10月13日 13:00:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64328791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定