使用泛型Object []的ClassCastException不能转换 [英] ClassCastException using Generics Object[] cannot be cast

查看:101
本文介绍了使用泛型Object []的ClassCastException不能转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图创建一个类,它使用一个数组来实现一个堆来创建一个优先级列表。在我的构造函数中,我想创建一个Entry对象数组。这可能吗?之前我已经做过一些通用铸造,并且我已经尝试了所有通常适用于我的方法。我觉得它应该是可能的,但我无法弄清楚。



这是运行它时得到的异常:


[Ljava.lang.Object;无法转换为[Llab09.Entry;




 `public class ArrayHeap< K,V>实施PriorityQueue< K,V> {
`私人比较器< K>补偿;
私人条目< K,V> []数据;
private int heapSize;
$ b @SuppressWarnings({unchecked})
public ArrayHeap(int size,Comparator< K> c){
data =(Entry< K,V> [])新对象[size]; //类型转换数组
heapSize = 0;
comp = c;
}
}

另外,我会将嵌套的Entry类放入

 受保护的静态类AHEntry 实现Entry< K,V> {
private K k;
private V v;

public AHEntry(K key,V value){
k = key;
v =价值;
}
public K getKey(){return k;}
public V getValue(){return v;}
public void setKey(K key){k = key;}
public void setValue(V value){v = value;}
}


data =(Entry< K,V> [])new Object [size] 会导致类型转换错误因为 Object 数组不能转换为 Map.Entry 数组。以下代码使用您提供的自定义 AHEntry 类:

  public class ArrayHeap< K,V>实施PriorityQueue< K,V> {
私人比较器< K>补偿;
私人条目< K,V> []数据;
private int heapSize;
$ b @SuppressWarnings({unchecked})
public ArrayHeap(int size,Comparator< K> c){
data = new(AHEntry< K,V>)new AHEntry<?,?> [size];
heapSize = 0;
comp = c;
}
}


So I'm trying to create a class that implements a heap using an array in order to make a prioritized list. In my constructor I want to create an array of Entry objects. Is this possible to do? I have done some generic casting before and I have tried everything that usually works for me. I feel like It should be possible but i can't figure it out.

This is the exception I get when I run it:

[Ljava.lang.Object; cannot be cast to [Llab09.Entry;

`public class ArrayHeap<K,V> implements PriorityQueue<K,V> {
    `private Comparator<K> comp;
     private Entry<K,V>[] data;
     private int heapSize;

     @SuppressWarnings({"unchecked"})
     public ArrayHeap(int size, Comparator<K> c){
        data = (Entry<K,V>[]) new Object[size]; // type casting array
        heapSize = 0;
        comp = c;
    }
 }

Also I'll throw in my nested Entry class to look at aswell.

protected static class AHEntry<K,V> implements Entry<K,V> {
    private K k;
    private V v;

    public AHEntry(K key, V value){
        k = key;
        v = value;
    }
    public K getKey(){ return k;}
    public V getValue(){ return v;}
    public void setKey(K key){ k = key;}
    public void setValue(V value){ v = value;}
}

解决方案

The line data = (Entry<K,V>[]) new Object[size] is resulting in a type cast error because an Object array cannot be cast to a Map.Entry array. The following code makes use of the custom AHEntry class which you provided:

public class ArrayHeap<K,V> implements PriorityQueue<K,V> {
    private Comparator<K> comp;
    private Entry<K,V>[] data;
    private int heapSize;

    @SuppressWarnings({"unchecked"})
    public ArrayHeap(int size, Comparator<K> c){
        data = new (AHEntry<K, V>)new AHEntry<?, ?>[size];
        heapSize = 0;
        comp = c;
    }
}

这篇关于使用泛型Object []的ClassCastException不能转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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