Java“对compareTo(T)作为原始类型java.lang.Comparable的成员进行未经检查的调用”

发布于 2024-10-14 16:07:06 字数 295 浏览 9 评论 0 原文

我正在尝试用 Java 来实现一个排序列表作为一个简单的练习。为了使其通用,我有一个 add(Comparable obj) ,这样我就可以将它与任何实现 Comparable 接口的类一起使用。

但是,当我在代码中的任何位置使用 obj.compareTo(...) 时,我得到“未经检查的调用compareTo(T) 作为原始类型 java.lang.Comparable 的成员” 来自编译器(使用 -Xlint:unchecked 选项)。该代码工作得很好,但我不知道如何摆脱那个烦人的消息。

有什么提示吗?

I'm trying to implement a sorted list as a simple exercise in Java. To make it generic I have an add(Comparable obj) so I can use it with any class that implements the Comparable interface.

But, when I use obj.compareTo(...) anywhere in the code I get "unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable" from the compiler (with -Xlint:unchecked option). The code works just fine but I can't figure out how to get rid of that annoying message.

Any hints?

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

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

发布评论

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

评论(3

烦人精 2024-10-21 16:07:06

本质上,此警告表示 Comparable 对象不能与任意对象进行比较。 Comparable 是一个通用接口,其中类型参数 T 指定可以与该对象进行比较的对象的类型。

因此,为了正确使用 Comparable,您需要使排序列表通用,以表达列表存储可以相互比较的对象的约束,如下所示:

public class SortedList<T extends Comparable<? super T>> {
    public void add(T obj) { ... }
    ...
}

In essence, this warning says that Comparable object can't be compared to arbitrary objects. Comparable<T> is a generic interface, where type parameter T specifies the type of the object this object can be compared to.

So, in order to use Comparable<T> correctly, you need to make your sorted list generic, to express a constraint that your list stores objects that can be compared to each other, something like this:

public class SortedList<T extends Comparable<? super T>> {
    public void add(T obj) { ... }
    ...
}
森林迷了鹿 2024-10-21 16:07:06

使用像 Comparable 这样的接口作为方法参数并不会使你的类变得通用,声明和使用通用类型参数才是使它通用的方法。

快速而肮脏的答案:您收到警告是因为您正在使用 Comparable(这是一个通用接口)作为原始类型,而不是给它一个特定的类型参数,例如Comparable

要解决此问题,请通过指定类型参数使 add() 变得通用:

<T extends Comparable<? super T>> add(T obj) { ... }

但是此快速修复无法解决类不安全的一般问题。毕竟,列表中的所有对象不应该属于同一类型吗?此 add 方法允许您将不同的类型添加到同一列表中。当您尝试比较异构类型时会发生什么(如何将 Object 实例与 Number 实例或 String 实例进行 compareTo )?您可以依赖类的用户做正确的事情,并确保他们只在您的列表中保留一种事物,但是泛型类将让编译器强制执行此规则。

更好的方法:正确的解决方法是,您的排序列表类总体上应该是通用的,就像 java.util

您可能会喜欢类似的内容:

public class SortedList<T extends Comparable<? super T>>
implements Iterable<T> {
    ...
    public void add(T item) { ... }
    public Iterator<T> iterator() { ... }
    ...
}

请注意,当类是泛型时,add 方法使用类的形式类型参数,而不是声明其自己的形式类型参数。

网上应该有很多关于如何创建泛型类的教程,但这里有一个简单的示例:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ002

class Pair<X,Y>  {  
  private X first; 
  private Y second;
  public Pair(X a1, Y a2) { 
    first  = a1; 
    second = a2; 
  } 
  public X getFirst()  { return first; } 
  public Y getSecond() { return second; } 
  public void setFirst(X arg)  { first = arg; } 
  public void setSecond(Y arg) { second = arg; } 
}

Using an interface like Comparable as a method parameter doesn't make your class generic, declaring and using generic type parameters is how you make it generic.

Quick-n-dirty answer: You are receiving the warning because you are using Comparable, which is a generic interface, as a raw type, rather than giving it a specific type arguments, like Comparable<String>.

To fix this, make add() generic by specifying type parameters:

<T extends Comparable<? super T>> add(T obj) { ... }

But this quick fix won't fix the general problem that your class is unsafe. After all, shouldn't all the objects in your list be of the same type? This add method lets you still different types into the same list. What happens when you try to compare heterogeneous types (how do you compareTo an Object instance to an Number instance, or to a String instance)? You can depend on the user of the class to do the right thing and ensure they only stick 1 kind of thing in your list, but a generic class will let the compiler enforce this rule.

The better approach: The proper fix is that your sorted list class should be probably be generic overall, just like the other collection classes in java.util.

You would probably like something like:

public class SortedList<T extends Comparable<? super T>>
implements Iterable<T> {
    ...
    public void add(T item) { ... }
    public Iterator<T> iterator() { ... }
    ...
}

Note that when the class is generic, the add method uses the classes formal type parameter rather than declaring its own formal type parameter.

There should be plenty of tutorials on the web on how to create a generic class, but here's a quick example:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ002

class Pair<X,Y>  {  
  private X first; 
  private Y second;
  public Pair(X a1, Y a2) { 
    first  = a1; 
    second = a2; 
  } 
  public X getFirst()  { return first; } 
  public Y getSecond() { return second; } 
  public void setFirst(X arg)  { first = arg; } 
  public void setSecond(Y arg) { second = arg; } 
}
猛虎独行 2024-10-21 16:07:06

您需要“检查”或定义 Comparable 对象,如下所示:

add(Comparable<Object> obj)

You need to "check" or define the Comparable object like so:

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