为什么我得到一个类转换异常(有仿制药,具有可比性)? [英] Why am i getting a class cast exception(with generics, comparable)?

查看:163
本文介绍了为什么我得到一个类转换异常(有仿制药,具有可比性)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个有序和无序数组列表。这两个扩展名为AbstractArrayMyList类,它包含常用的操作/实现 - 的toString,清晰,等...

I am trying to implement a sorted and unsorted array list. Both extend a class called AbstractArrayMyList which contains common operations/implementations - toString, clear, etc....

下面是我的code为AbstractArrayMyList(实现我定义了一个通用接口)

Here is my code for AbstractArrayMyList(which implements a generic interface I defined)

public abstract class AbstractArrayMyList<E> implements MyList<E> {
        protected E[] elementData;
       .....
}

我选择从elementData做保护,这样排序和不排序专门的数组列表可以访问并在其上执行操作。这里是我的声明/ code为排序的数组列表

I chose to make elementData protected so that sorted and unsorted specialized array lists can access and perform operations on it. Here is my declaration/code for sorted array list

public class ArrayListSorted<E extends Comparable<E>> extends AbstractArrayMyList<E> 

这一切编译罚款。然而,当我测试我的code,这些线路

This all compiles fine. However when I test my code, with these lines

ArrayListSorted<Integer> toTestInteger = new ArrayListSorted<Integer>()
toTestInteger.insert(0);
assertEquals(toTestInteger.get(0).intValue(), 0);

我得到一个类转换异常

I get a class cast exception

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    at myarraylist.ArrayListSorted.getIndex(ArrayListSorted.java:38)

这是发生在这里。

@Override
public int getIndex(E value) {
     int lo = 0;
     int hi = size;
     while (lo <= hi) {
         // Key is in a[lo..hi] or not present.
         int mid = lo + (hi - lo) / 2;
         if      (value.compareTo(elementData[mid]) < 0)  hi = mid - 1;

在同一行的compareTo发生异常。有谁知道问题是什么?我定义了绑定的通配符,E扩展可比,这意味着希望与ArrayListSorted工作的任何类必须实现Comparable接口...

The exception occurs on the same line as the compareTo. Does anyone know what the issue is? I defined the bounded wildcard, E extends Comparable, which means that any class that wishes to work with ArrayListSorted must implement the Comparable interface...

我的意思是我甚至有正确的语法,从的http:// docs.oracle.com/javase/tutorial/java/generics/upperBounded.html ,类型扩展类/接口

I mean i even have the right syntax, from http://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html, type extends class/interface

推荐答案

的问题是,你使用泛型类型的数组类型。数组类型的具体化的(实际上美元的JVM p $ psent)在运行时,但泛型类型都没有。这意味着你的新E [] 其实最终是一个对象[] ,而不是类型的数组你想要的。

The problem is that you're using the generic type as the type of the array. Array types are reified (actually present in the JVM) at runtime, but the generic types aren't. This means that your new E[] actually ends up being an Object[] instead of an array of the type you wanted.

标准馆藏处理这个问题通过不提供对阵列的直接接入和铸造电子 get操作()。如果你真的觉得用一个类型数组是最好的选择,那么你就需要通过类&LT; E&GT; clazz所来构造你的抽象基类,并用它来构建一个正确类型的数组:

The standard collections deal with this problem by not providing direct access to the array and casting to E on operations like get(). If you really think that using a typed array is the best option, then you'll need to pass Class<E> clazz to the constructor for your abstract base class and use that to construct a correctly-typed array:

protected AbstractArrayMyList(Class<E> clazz) {
    this.elementClass = clazz;
    this.elementData = Array.newInstance(clazz, INITIAL_SIZE);
}

原因你得到 ClassCastException异常是编译器替换方法签名与擦除,这基本上是可以接受的类型的最大公约数。既然你缩小电子对象可比中你的子类,在这种方法的签名最终被可比[] 而不是对象[]

The reason you're getting the ClassCastException is that the compiler replaces the method signatures with their erasures, which is basically the greatest common denominator of the acceptable types. Since you're narrowing E from Object to Comparable in your subclass, the signature on that method ends up being Comparable[] instead of Object[].

这篇关于为什么我得到一个类转换异常(有仿制药,具有可比性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆