【问题标题】:Java: "uses unchecked or unsafe operations. Recompile with..."Java:“使用未经检查或不安全的操作。重新编译...”
【发布时间】:2019-07-12 22:06:16
【问题描述】:

尽管花费大量时间在谷歌上搜索我的困境的答案并重新阅读我的 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.

几乎所有 I here 或 Comparable there 的组合都会导致这样的弹出窗口。我正在使用 BlueJ 环境进行编程,因此无法合并相关的编译器参数以查看任何详细信息。

据我到目前为止的研究,这与内部类使用 I 泛型类型有关,因此 RedBlackNode 内部类中的“RedBlackNode 实现 Comparable”和 compareTo 方法需要以某种方式应对这个事实。

我知道这个问题已经在 stackoverflow 和其他地方被问过,并且回答了很多次,但我似乎无法将从这些实例中学到的知识应用到我的案例中。我对泛型很陌生,所以我能在这里得到的任何帮助都将不胜感激!

【问题讨论】:

    标签: java inner-classes instanceof comparable nested-generics


    【解决方案1】:

    进行以下更改

    public class RedBlackTree<I extends Comparable<I>>
    
    private class RedBlackNode implements Comparable<RedBlackNode>
    
        @Override
        public int compareTo(RedBlackNode node)
                int result = id.compareTo(node.id);
                return result > 0 ? 1 : result < 0 ? -1 : 0;
    

    在 compareTo 中移除类型检查。请注意公众,因此请始终使用@Override

    【讨论】:

    • 谢谢!其他答案当然有帮助,但这个答案解决了所有问题(包括在覆盖 compareTo 函数时注意到公共/私人困境,我认为这可能会导致问题,但其他人尚未提及)。
    【解决方案2】:

    Comparable 采用类型参数。编译器抱怨是因为你没有提供它。

    【讨论】:

    • 是的,但是即使我为它提供了一个类型(例如 ),我仍然会遇到各种问题...
    • 您的代码中有两个地方在使用 Comparable 而没有类型信息。您是否尝试过在两个地方都添加类型信息?如果您添加了有关您尝试过的示例,将会很有用。
    • 当我输入“class RedBlackNode implements Comparable”时,我仍然收到警告。另外,这个“public class RedBlackTree>”是否更适合外部类声明?
    【解决方案3】:

    警告是因为您使用的是原始类型 Comparable 而不是给它一个类型参数。您所要做的就是将类定义更改为

    public class RedBlackTree<I extends Comparable<I>>
    

    private class RedBlackNode implements Comparable<RedBlackNode>
    

    并相应地调整compareTo() 方法(这实际上会大大简化它,因为您不再需要类型检查和强制转换)。

    【讨论】:

    • 好的,这就是我现在所拥有的(减去 compareTo() 方法的“调整”)。你会建议我如何“调整”它?这是否合适:“private int compareTo(RedBlackNode obj)”?现在(经过调整)我仍然收到警告,它指向“私有类 RedBlackNode 实现 Comparable”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多