Java:对compareTo(T)的未经检查的调用

发布于 2024-11-27 22:11:10 字数 1877 浏览 1 评论 0原文

 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  }

编译上面的代码会产生 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

我尝试了更多变体,但警告仍然存在。编写和调用上面的 test.compare 方法的正确方法是什么?

谢谢!

PS:test.compare只是一个例子;我不需要这样的功能;但我需要实现一个函数,就像 test.compare 一样,需要在其签名中包含可比较的实现对象。

PS2:我已经编程了 25 年以上,大约 10 年前我什至编程了一段时间 Java,但现在使用 Java(由于我的工作需要)让我发疯。对于经验丰富的程序员来说,学习 Java 比看起来要困难得多。有这么多\ 那里有很多关于学习 Java 的东西,其中 99% 充其量是过时的,或者是为了给编程新手排名(即非常冗长),最坏的情况是彻头彻尾的垃圾……我还没有找到关于 Java 的参考资料,可以让我很快就把上面问题的答案归零了。

 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:

% 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?

Thanks!

PS: test.compare is only an example; 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.

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. For an experienced programmer, learning Java is much harder than it looks. There's so mu\
ch stuff on learning Java out there, 99% of which is at best outdated, or pitched to rank programming novices (i.e. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

勿忘心安 2024-12-04 22:11:10

您应该使用通用参数声明 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));
    }
}

You should declare the compare method with a generic argument.

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));
    }
}
简美 2024-12-04 22:11:10

可比较是通用 - 您应该使用 Comparable 定义变量

Comparable is generic - you should define the variables with Comparable<Integer>

一个人的旅程 2024-12-04 22:11:10

真正的问题是您试图在静态方法中进行比较。使进行比较的方法成为非静态的,实例化一个或多个“测试器”对象并为每个对象提交一种类型。例如:

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

然后调用 String 对象的比较方法:

 int retCode = strTester.comp(a, b)

如果您想比较其他类型的对象(例如 Integers),您将需要一个新的测试器对象,例如:

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

如果您愿意这样做,那么您的类可以沿着行:

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

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. For example:

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

then to invoke the comparison method for String objects:

 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) {
           ...
      }
 }
绅刃 2024-12-04 22:11:10

zeroone 声明为 IntegerComparable 而不是原始类型。

Angelika Langer 制作了关于仿制药的一个很好的参考。它被组织为分层常见问题解答,并且应该允许您可以快速找到大多数通用类型问题的具体答案。在这种情况下,您可能会发现有关原始类型的部分很有用。

Declare zero and one as Integer or Comparable<Integer> rather than raw types.

Angelika Langer has produced a great reference for generics. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文