I have the following (partial) class:
public class Graph {
private ArrayList edges;
public ArrayList getEdges() {
return edges;
}
}
Now, when calling the method getEdges() somewhere else and storing the result in a variable of type ArrayList, I get warning: [unchecked] unchecked conversion:
OtherFile.java:101: warning: [unchecked] unchecked conversion
ArrayList edges = graph.getEdges();
^
required: ArrayList
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, so why can't I store its result in a variable of that exact type?
解决方案
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.