简体   繁体   English

使用通用自定义类时的ClassCastException

[英]ClassCastException when using generic custom class

I was trying to construct a simple list class using generics. 我试图使用泛型构造一个简单的列表类。
However, it throws classcastexception when I was trying to print out the value. 但是,当我尝试打印该值时,它将引发classcastexception。 Is there any problem when I declare and initialize generic array? 声明和初始化通用数组时有什么问题吗?

class vector<E> {
    static int MAX_LEN = 1234567;
    E[] data;
    int[] prv;
    int to;
    int size;

    vector() {
        data = (E[])new Object[MAX_LEN];
        prv = new int[MAX_LEN];
        to = -1;
        size = 0;

        for(int i = 0; i < MAX_LEN; ++i) {
            prv[i] = -1;
        }
    }

    void push_back(E e) {
        data[size] = e;
        prv[size] = to;
        to = size;
        ++size;
    }
}

public class Main {

    public static void main(String[] args) throws Exception {
        vector<Integer> v = new vector();
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);

        for(int i = v.to; i != -1; i = v.prv[i]) {
            System.out.println(v.data[i]); //<- Exception here
        }
    }
}

There are a few things wrong with the code. 该代码有一些错误。

  1. It looks like, you've missed a <> in the line vector<Integer> v = new vector(); 看起来,你已经错过了<>在该行vector<Integer> v = new vector();
  2. A cast from Object[] to Integer[] will never succeed Object[]Integer[]永远不会成功

Ex: 例如:

Integer[] a= (Integer[]) new Object[] {}; Integer [] a =(Integer [])new Object [] {}; // this cast will never succeed //此强制转换永远不会成功

Infact you can only cast Object[] to Object or Object[] . 实际上,您只能将Object[]强制转换为ObjectObject[]

currently when you are trying to access data in the main() method, the cast put in by the compiler for you Integer[] will fail at runtime. 目前,当您试图访问datamain()方法,剧组投入编译器为您Integer[]会在运行时失败。 You don't need the data array, you just need the values from it, which you can get by adding the following method to the vector class. 您不需要data数组,只需要data数组中的值,可以通过将以下方法添加到vector类中来获得data数组。

E getData(int index) {
    return data[index];
}

In this case, compiler will add the cast to individual elements and will provide you the correct values. 在这种情况下,编译器将强制类型转换添加到各个元素,并为您提供正确的值。

You really have two options here. 您这里确实有两个选择。 Either use data array of Object[] type or create proper generic array. 使用Object[]类型的data数组或创建适当的通用数组。 Here is how to create generic array. 这是创建通用数组的方法。

    vector(Class<E> clazz) {
        data = (E[])Array.newInstance(clazz, MAX_LEN);

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

相关问题 扩展泛型类时出现ClassCastException - ClassCastException when extending a generic class 尝试获取泛型类时ClassCastException - ClassCastException when trying to get the generic type class 将类泛型声明为字符串时的 ClassCastException - ClassCastException when declaring class generic as string 使用Mockito模拟泛型类型时的ClassCastException - ClassCastException when mocking generic type using Mockito Java AtomicReferenceFieldUpdater-使用泛型类型时的ClassCastException - Java AtomicReferenceFieldUpdater - ClassCastException when using generic types Mybatis在使用自定义泛型typehandler并且bean属性名和表的列相同时,select会抛出ClassCastException - Mybatis when using custom generic typehandler and the bean property name and table's column are same, select will raise ClassCastException 使用自定义ArrayAdapter时获取ClassCastException - Getting ClassCastException when using custom ArrayAdapter 将 Object 转换为 String 时出现 ClassCastException,但将 Object 转换为自定义类时没有 ClassCastException - ClassCastException when casting Object to String, but no ClassCastException when casting Object to Custom class 使用Mockito作为通用方法的ClassCastException - ClassCastException using Mockito for generic method AsyncItemWriter - 将自定义 class 解包到 Future 时遇到 ClassCastException - AsyncItemWriter - Facing ClassCastException when unwrapping custom class into Future
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM