Java warning: [unchecked] unchecked conversion

痴心易碎 提交于 2019-12-30 12:38:48

问题


I have the following (partial) class:

public class Graph<O> {
    private ArrayList<Edge> edges;

    public ArrayList<Edge> getEdges() {
        return edges;
    }
}

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/22744197/java-warning-unchecked-unchecked-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!