英文:
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<Object> source = Arrays.asList(1,2,3);
List<Number> 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<Number>) (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 <T> void copy(List<? super T> dest, List<? extends T> 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<? super T>,List<? extends T>
found: List<Number>,List<Object>
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 <T>copy(List<? super T>,List<? extends T>)
答案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 <T> void copy(List<? super T> dest, List<? extends T> src) {}
you find out that both arguments should be List
and dest
should be higher in the hierarhy than src
. In your example - Number <- 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<Object> source = Arrays.asList(1,2,3,"string allowed here");
List<Number> dest = Arrays.asList(4.1,5,6,9.1);
Collections.copy(dest, source);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论