简体   繁体   English

Java:“使用未经检查或不安全的操作。 重新编译……”

[英]Java: “uses unchecked or unsafe operations. Recompile with…”

Despite spending a significant amount of time googling for an answer to my predicament and re-reading the chapter on Generics in my Java textbook I cannot seem to fix the problems with the following code: 尽管花了很多时间来寻找解决我的困境的方法,然后重新阅读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.");
            }
        }
    }
}

In particular, I'm receiving a popup with the following message: 特别是,我收到带有以下消息的弹出窗口:

Warnings from last compilation

C:\Users\...\RedBlackTree.java uses unchecked or unsafe operations.
Recompile with -Xlint:unchecked for details.

Nearly every combination of I here or Comparable there still leads to such a popup. 这里的I或可比较的I的几乎每种组合仍然会导致这样的弹出窗口。 I'm using the BlueJ environment for programming and this makes it impossible to incorporate the relevant compiler argument in order to see any details. 我使用BlueJ环境进行编程,因此无法合并相关的编译器参数以查看任何详细信息。

As far as I can discern from my research thus far, it's something to do with the fact that the inner class utilizes the I generic type and so the "RedBlackNode implements Comparable" and the compareTo method in the RedBlackNode inner class need to contend with that fact somehow. 就目前为止的研究来看,这与以下事实有关:内部类使用I泛型类型,因此RedBlackNode内部类中的“ RedBlackNode实现Comparable”和compareTo方法需要与之抗衡事实不知何故。

I know that this question has been asked here on stackoverflow and elsewhere and answered many times, but I can't seem to apply what I've learned from those instances to my case. 我知道这个问题已经在stackoverflow和其他地方被问到并回答了很多次,但是我似乎无法将从这些实例中学到的知识应用于我的案例。 I'm pretty new to Generics, so any help I can get here would be very much appreciated! 我对Generics来说还很陌生,所以我能在这里获得的任何帮助将非常感激!

Comparable takes a Type parameter. Comparable采用Type参数。 The compiler complains because you have not supplied it. 编译器抱怨,因为您没有提供它。

The warning is because you're using the raw type Comparable instead of giving it a type parameter. 警告是因为您使用的是原始类型Comparable而不是为其提供类型参数。 All you have to do is change the class definitions to 您要做的就是将类定义更改为

public class RedBlackTree<I extends Comparable<I>>

and

private class RedBlackNode implements Comparable<RedBlackNode>

and adjust the compareTo() method accordingly (which will actually simplify it considerably since you don't need the type checking and casting anymore). 并相应地调整compareTo()方法(实际上将大大简化该方法,因为您不再需要类型检查和转换了)。

Make the following changes 进行以下更改

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;

In the compareTo remove type checking. 在compareTo中删除类型检查。 Mind the public, therefore always use @Override . 注意公众,因此请始终使用@Override

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .java使用未经检查或不安全的操作。 注意:使用-Xlint重新编译:未检查详细信息不确定代码的哪一部分 - .java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details Not sure which part of code 注意:Anpr.java 使用未经检查或不安全的操作。 注意:使用 -Xlint 重新编译:未选中 JComboBox 中的详细警告 - Note: Anpr.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details warning in JComboBox 注意:test.java 使用未经检查或不安全的操作。 注意:使用 -Xlint:unchecked 重新编译以获取详细信息 - Note: test.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details 使用未经检查或不安全的操作,使用 -Xlint 重新编译 - Uses unchecked or unsafe operations, recompile with -Xlint 程序使用未经检查或不安全的操作,请使用-Xlint:uncheck重新编译以获取详细信息 - Program uses unchecked or unsafe operations, recompile using -Xlint:unchecked for details 未经检查或不安全的操作。 数组到JList - Unchecked or unsafe operations. array to JList class.java使用未经检查或不安全的操作 - class.java uses unchecked or unsafe operations InventoryItem.java使用未经检查或不安全的操作 - InventoryItem.java uses unchecked or unsafe operations Main.java使用未经检查或不安全的操作 - Main.java uses unchecked or unsafe operations MergeSort.java使用未经检查或不安全的操作 - MergeSort.java uses unchecked or unsafe operations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM