尽管花了大量的时间搜索一个答案,我的困境和重新阅读我的Java教科书中泛型的一章,似乎无法修复问题下面的代码:Java:“使用未经检查或不安全的操作。使用...重新编译”
public class RedBlackTree<I extends Comparable>
{
private int count = 0;
private RedBlackNode root;
private RedBlackNode current;
private RedBlackNode[] stack;
/**
* Inner class used for representing the nodes of the red-black balanced binary search tree object.
*/
private class RedBlackNode implements Comparable
{
private I id;
private boolean is_btree;
private RedBlackNode[] links;
/**
* Constructor for objects of the RedBlackNode class.
*
* @param id The ID of the node.
*/
private RedBlackNode(I id)
{
if (id == null)
{
throw new NullPointerException("ID cannot be null.");
}
this.id = id;
this.is_btree = true;
}
/**
* Function for comparing the RedBlackNode object to another object.
*
* @param obj The object to be compared.
* @return If invocant > passed, returns 1; if invocant < passed, returns -1; if invocant = passed, returns 0.
*/
private int compareTo(Object obj)
{
if (obj instanceof RedBlackTree.RedBlackNode)
{
RedBlackNode node = (RedBlackNode)obj;
int result = id.compareTo(node.id);
return result > 0 ? 1 : result < 0 ? -1 : 0;
}
else
{
throw new ClassCastException("Expected a RedBlackNode object.");
}
}
}
}
特别是,我收到一个弹出以下消息:
Warnings from last compilation
C:\Users\...\RedBlackTree.java uses unchecked or unsafe operations.
Recompile with -Xlint:unchecked for details.
几乎所有的我在这里组合或类似的还有导致这样的弹出窗口。我正在使用BlueJ环境进行编程,这使得无法合并相关编译器参数以查看任何细节。
据我迄今为止从我的研究中可以看出,这与内部类使用I泛型类型以及“RedBlackNode实现Comparable”以及RedBlackNode内部类中的compareTo方法需要相关以某种方式与这个事实相抗衡。
我知道这个问题已经在stackoverflow和其他地方问过了,并且回答了很多次,但我似乎无法将从这些实例中学到的东西应用到我的案例中。我对泛型非常新,所以我可以得到的任何帮助将非常感谢!
谢谢!其他答案当然有帮助,但这个答案解决了所有问题(包括注意到重写compareTo函数的公共/私人困境,我认为这可能会导致一个问题,但没有人提到过)。 – sweetname 2013-03-21 11:21:10