Java警告:[unchecked]未经检查的转换 [英] Java warning: [unchecked] unchecked conversion
问题描述
我有以下(部分)类:
I have the following (partial) class:
public class Graph<O> {
private ArrayList<Edge> edges;
public ArrayList<Edge> getEdges() {
return edges;
}
}
现在,当调用方法 getEdges()
>并将结果存储在类型为 ArrayList< Edge>
的变量中,得到警告: [unchecked] unchecked conversion
:
Now, when calling the method getEdges()
somewhere else and storing the result in a variable of type ArrayList<Edge>
, I get warning: [unchecked] unchecked conversion
:
OtherFile.java:101: warning: [unchecked] unchecked conversion
ArrayList<Edge> edges = graph.getEdges();
^
required: ArrayList<Edge>
found: ArrayList
我已经看过关于此警告的其他多个问题,但我可以弄清楚我做错了什么。 getEdges()
返回 ArrayList< Edge>
,为什么我不能将它的结果存储在一个精确的变量中类型?
I have looked at multiple other questions about this warning, but I can't figure out what I'm doing wrong. getEdges()
returns ArrayList<Edge>
, so why can't I store its result in a variable of that exact type?
推荐答案
当您调用 getEdges()
方法对原始类型图
。在这种情况下,所有的通用类型都会被其擦除所替代。因此,对于 Graph
原始类型,方法签名变为:
That warning would come when you invoke the getEdges()
method on raw type Graph
. When that is the case, all the generic types are replaced with their erasure. So, for Graph
raw type, the method signature becomes like:
public ArrayList getEdges();
解决方案是使用参数化类型或通配符类型。
Solution is to use parameterized type or wildcard types.
这篇关于Java警告:[unchecked]未经检查的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!