简体   繁体   English

Java:unchecked调用compareTo(T)

[英]Java: unchecked call to compareTo(T)

 1  class test {
 2      public static int compare0(Comparable x, Comparable y) {
 3          return x.compareTo(y);
 4      }

 5      public static int compare1(Object x, Object y) {
 6          return ((Comparable) x).compareTo((Comparable) y);
 7      }

 8      public static int compare2(Object x, Object y) {
 9          Comparable p = (Comparable) x;
10          Comparable q = (Comparable) y;
11          return (p).compareTo(q);
12      }

13      public static void main(String[] args) {
14          Comparable zero = new Integer(0);
15          Comparable one = new Integer(1);
16          int c = (zero).compareTo(one);
17      }
18  }

Compiling the code above produces 4 warnings: 编译上面的代码会产生4个警告:

% javac -Xlint:unchecked test.java
test.java:3: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
    return x.compareTo(y);
                      ^
test.java:7: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
    return ((Comparable) x).compareTo((Comparable) y);
                                     ^
test.java:13: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
    return (p).compareTo(q);
                        ^
test.java:19: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
    int c = (zero).compareTo(one);
                            ^
4 warnings

I tried many more variants, but the warnings remain. 我尝试了更多的变种,但警告仍然存在。 What's the correct way to write and call the test.compare method above? 编写和调用上面的test.compare方法的正确方法是什么?

Thanks! 谢谢!

PS: test.compare is only an example; PS:test.compare只是一个例子; I don't need such a function; 我不需要这样的功能; but I need to implement a function that, like test.compare, needs to have Comparable-implementing objects in its signature. 但我需要实现一个函数,就像test.compare一样,需要在其签名中包含Comparable实现对象。

PS2: I've been programming for 25+ years, I even programmed Java for a while around 10 years ago, but using Java now (required by my job) is driving me berserk. PS2:我已经编程了25年以上,我甚至在大约10年前编写了Java一段时间,但现在使用Java(我的工作需要)让我疯狂。 For an experienced programmer, learning Java is much harder than it looks. 对于有经验的程序员来说,学习Java比看起来要困难得多。 There's so mu\\ ch stuff on learning Java out there, 99% of which is at best outdated, or pitched to rank programming novices (ie massively verbose), and at worst is outright garbage... I have yet to find a reference on Java that will let me quickly zero in the answer to the question above. 在那里学习Java有很多东西,其中99%最好是过时的,或者倾向于对编程新手进行排名(即大量冗长),最糟糕的是直接垃圾...我还没有找到关于Java将让我快速回答上述问题的答案。

Comparable是通用的 - 您应该使用Comparable<Integer>定义变量

You should declare the compare method with a generic argument. 您应该使用泛型参数声明compare方法。

public class ThisTest
{
    public static <T extends Comparable<T>> int compare(T x, T y) {
        if (x == null) 
            return -(y.compareTo(x));
        return x.compareTo(y);
    }

    public static void main()
    {
        // Type inferred
        int c = compare(Integer.valueOf(0), Integer.valueOf(1));
        // Explicit generic type parameter
        c = ThisTest.<Integer>compare(Integer.valueOf(0), Integer.valueOf(1));
    }
}

The real problem is that you are trying to do your comparisons within a static method. 真正的问题是你试图在静态方法中进行比较。 Make the method that does the comparison non-static, instantiate one or more "tester" objects and commit to a type for each. 使进行比较的方法非静态,实例化一个或多个“tester”对象并为每个对象提交一个类型。 For example: 例如:

 test<String> strTester = new test<String>();

then to invoke the comparison method for String objects: 然后调用String对象的比较方法:

 int retCode = strTester.comp(a, b)

If you want to compare other types of objects such as Integers you will need a new tester object like: 如果要比较其他类型的对象(如整数),则需要一个新的测试对象,如:

 test<Integer> intTester = new test<Integer>();

If you are willing to do this, then your class can be defined along the lines of: 如果您愿意这样做,那么您的课程可以按照以下方式定义:

 class test<T extends Comparable<T>> {
      public int comp(T x, T y) {
           ...
      }
 }

Declare zero and one as Integer or Comparable<Integer> rather than raw types. zeroone声明为IntegerComparable<Integer>而不是原始类型。

Angelika Langer has produced a great reference for generics. Angelika Langer 为仿制药提供了很好的参考。 It's organized as a hierarchical FAQ and should allow you to find specific answers to most generic type problems quickly. 它被组织为分层常见问题解答,应该允许您快速找到大多数通用类型问题的具体答案。 In this case, you may find the section on raw types useful. 在这种情况下,您可能会发现有关原始类型部分很有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM