简体   繁体   English

在通用类型“ T”的对象和通用类型“ <T extends Comparable<T> &gt;”

[英]Casting between an object of generic type “T” and an object of generic type “<T extends Comparable<T>>”

I've implemented an ArrayLinearList class on the following way 我已经通过以下方式实现了ArrayLinearList类

public class ArrayLinearList<T> implements LinearList<T>, Iterable<T>

which contains the class member 其中包含班级成员

protected T[] element

In order to extend the functionality of the class I create a new class that inherits from the first one 为了扩展该类的功能,我创建了一个新类,该类继承自第一个类

class SuperList<T extends Comparable<T>> extends ArrayLinearList<T>

Here I ensure that the type T implements the Comparable interface (this cannot be changed). 在这里,我确保类型T实现Comparable接口(无法更改)。

In the SuperList class I have the following method 在SuperList类中,我有以下方法

public void replace( int pedro, T maria ){
   element[ pedro ] = maria;

} }

The line of the method generate the following error 该方法的行产生以下错误

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;

But if I declare the class on the following way I don't have this problem anymore (but in this way I'm no longer able to compare class objects) 但是,如果我通过以下方式声明该类,那么我将不再遇到这个问题(但是通过这种方式,我将无法再比较类对象)

class SuperList<T> extends ArrayLinearList<T>{

I would appreciate any help to solve this issue. 对于解决此问题的帮助,我将不胜感激。 I'm not allowed to modify the class \\ [ ArrayLinearList, just the class SuperList. 我不允许修改类\\ [ArrayLinearList,仅可以修改类SuperList。

Thanks in advance. 提前致谢。

Based on the exception, it looks like you're initializing element with new Object[someLength] and doing an unchecked cast to T[] . 根据异常,您似乎正在使用new Object[someLength]初始化elementnew Object[someLength] T[]进行未经检查的转换。 This is likely happening in ArrayLinearList . 这很可能在ArrayLinearList发生。 The problem is that in this code: 问题是在这段代码中:

element[ pedro ] = maria;

element is treated as a Comparable[] at runtime, and an Object[] isn't a Comparable[] . element在运行时被视为Comparable[] ,而Object[]不是Comparable[]

There are many existing posts related to generic array creation, but your issue is interesting because you've thrown inheritance into the mix (with a narrowing of a type parameter's bounds). 现有许多与泛型数组创建相关的文章,但是您的问题很有趣,因为您已经将继承引入了混合(缩小了类型参数的范围)。 The best solution seems to be only interacting with element within the ArrayLinearList class. 最好的解决方案似乎只是与ArrayLinearList类中的element进行交互。 Make it private and expose getters and setters. 将其设为private并公开获取器和设置器。 This should avoid runtime failures like this (which take some thought to avoid when doing unchecked casts like (T[])new Object[someLength] ). 这应该避免这样的运行时失败(在进行诸如(T[])new Object[someLength]类的未经检查的强制转换时,应考虑一些避免的事情)。

For example: 例如:

public class ArrayLinearList<T> implements LinearList<T>, Iterable<T> {

    private final T[] elements;

    public ArrayLinearList(final int length) {
        @SuppressWarnings("unchecked") // won't be exposed outside this class
        final T[] withNarrowedType = (T[])new Object[length];
        elements = withNarrowedType;
    }

    public final void set(final int index, final T element) {
        elements[index] = element;
    }
}

EDIT: I just noticed your disclaimer "I'm not allowed to modify the class ArrayLinearList just the class SuperList ." 编辑:我只注意到你的声明:“我不允许修改类ArrayLinearList只是类SuperList 。” Well that's unfortunate, because the author of ArrayLinearList has written type-unsafe code. 好吧,这很不幸,因为ArrayLinearList的作者编写了类型不安全的代码。 But the question becomes why a method like SuperList.replace needs to interact with the array directly (or exist at all). 但是问题变成了为什么像SuperList.replace这样的方法需要直接与数组交互(或根本不存在)。 It looks like it's simply assigning a new element to the specified index - is there no ArrayLinearList method you can use to do that? 看起来好像只是向指定的索引分配了一个新元素-是否可以使用ArrayLinearList方法执行此操作?

If all else fails, here's a last-ditch workaround: 如果所有其他方法均失败,那么这是最后的解决方法:

((Object[])element)[pedro] = maria;

But that's an awful solution that duct-tapes over the underlying problem. 但这是解决潜在问题的可怕解决方案。

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

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