在写泛型的BST时,javac -xlint test.java,出现Warning: Unchecked or unsafe operations。
public E next() {
Node nextNode=null;
boolean flag=true;
while(flag) {
if (current != null) {
stack.push(current);
current = current.left;
} else {
current = stack.peek();
if (current.right != null && pre != current.right) {
current = current.right;
} else
flag=false;
}
}
pre=stack.pop();
current=pre;
nextNode=current;
current=null;
return (E)nextNode.val;
}
出错语句:return (E)nextNode.val;
提示没有检查类型。
这里是java设计的问题。加一句@SuppressWarnings("unchecked")
即可。
另外还有一段也报同样的错。因为我extends了comparable,重写了equals。这里如果没有重写hashCode就会继续报警告,提示重写了equals而没有写hashCode。
为什么重写一个就要重写另一个?
是因为Java规定,如果a.equals(b)==0,则a.hashCode()==b.hashCode();