我不明白为什么在以下情况下Java编译器会向我发出“未经检查的转换”警告:
我有这个课:
public class NodeTree<T> {
T value;
NodeTree parent;
List<NodeTree<T>> childs;
NodeTree(T value, NodeTree parent) {
this.value = value;
this.parent = parent;
this.childs = null;
}
public T getValue() { return value; }
public void setValue(T value) { this.value = value; }
public NodeTree getParent() { return parent; }
public void setParent(NodeTree parent) { this.parent = parent; }
public List<NodeTree<T>> getChilds() {
if (this.childs == null) {
this.childs = new LinkedList<NodeTree<T>>();
}
return this.childs;
}
}
在主要课程中,我有以下说明:
NodeTree node = new NodeTree<Integer>(10, null);
NodeTree<Integer> child = new NodeTree<Integer>(20, node);
List<NodeTree<Integer>> childs = node.getChilds();
childs.add(child);
我无法解释为什么我在这种类型的getChilds()行上得到警告:
warning: [unchecked] unchecked conversion
List<NodeTree<Integer>> childs = node.getChilds();
^
required: List<NodeTree<Integer>>
found: List
1 warning
getChilds()函数不返回List类型,它返回List >类型。
请帮助我理解。
最佳答案
编写NodeTree<Integer> node = new NodeTree<>(10, null);
会更好吗
代替NodeTree node = new NodeTree<Integer>(10, null);
?然后,编译器将知道node
的类型参数。