当直接引用泛型数组抛出ClassCastException异常(不通过辅助方法,当调用) [英] Generic array throws ClassCastException when referenced directly (it doesn't when calling through helper method)

查看:170
本文介绍了当直接引用泛型数组抛出ClassCastException异常(不通过辅助方法,当调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是魔术!看看这个简单的code:

This is magic! Look at this simple code:

public class ArrayOFMagic<T> {

    protected T[] array;

    protected int showMeYouRLength() {
        return array.length;
    }

    ArrayOFMagic() {
        array = (T[]) new Object[10];
    }

    protected void set(T value, int index) {
        array[index] = value;
    }

    public static void main(String[] args) {
        ArrayOFMagic<Integer> arrayOFMagic = new ArrayOFMagic<Integer>();
        System.out.println(arrayOFMagic.showMeYouRLength());
        System.out.println("MAGIC INCOMING");
        System.out.println(arrayOFMagic.array.length);
    }

}

输出:

10结果MAGIC来电结果在线程异常主
  java.lang.ClassCastException:[Ljava.lang.Object;不能被转换为
  [Ljava.lang.Integer;在ArrayOFMagic.main(ArrayOFMagic.java:25)

10
MAGIC INCOMING
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; at ArrayOFMagic.main(ArrayOFMagic.java:25)

我叫array.length两次。一旦过法,一次直接。它的使用方法时,并进行直接调用时抛出异常。 O.o有人解释一下吗?

I call array.length two times. Once through method and once directly. It proceeds when using method and throws exception when called directly. O.o Someone explain?

编辑:
只是为了澄清:类的工作很好,当不直接调用。您可以设置器/吸气的数组元素和所有..!

edit: Just to clarify: Class works good when not called directly. You can have setters/getters on array elements and all..!

推荐答案

修订答:

免责声明:这个答案是不是从我,我问了前Sun员工谁在Java中仿制药的工作为什么发生这种情况。他的回答是:

DISCLAIMER: This answer is not from me, I asked a former Sun employee who worked on generics in Java why this happens. His answer:

第一个调用访问从通用类本身,它被删除到它的下限(在这种情况下,对象)里面的成员,所以有一个在裁判array.length没有投。

The first call accesses the member from inside the generic class itself, which is erased to it's lower bound (in this case, Object), so there's no cast in the ref to array.length.

但第二个呼叫在的参数实例的一个的泛型类型的,所以类型变量(数组)被绑定到整数。

But the second call is on a parameterized instance of a generic type, so the type variable (the array) is bound to Integer.

数组字段被声明为类型T [],这势必整数[]的。由编译器发出的访问code将其投射到该类型和投击(在每一个词的意义。)

The array field is declared to be of type T[], which is bound to Integer[]. The accessor code that is emitted by the compiler casts it to that type, and the cast blows (in every sense of the word.)

OLD答:
这里是实现你的类(作为除zhong.j.yu的答案)更好的方式。

OLD ANSWER: Here's a better way to implement your class (as an addition to zhong.j.yu's answer).

import java.util.ArrayList;
import java.util.List;

public class NotSoMagical<T> {

    public List<T> arrayList;

    protected int length() {
        return arrayList.size();
    }

    NotSoMagical() {
        arrayList = new ArrayList<T>(10);
    }

    protected void set(T value, int index) {
        arrayList.set(index, value);
    }

    public static void main(String[] args) {
        NotSoMagical<Integer> notMagicalAtAll = new NotSoMagical<Integer>();
        System.out.println(notMagicalAtAll.length());
        System.out.println("MAGIC INCOMING");
        System.out.println(notMagicalAtAll.arrayList.size());
    }

}

这篇关于当直接引用泛型数组抛出ClassCastException异常(不通过辅助方法,当调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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