为什么我会收到Class Cast Exception?

时间:2015-06-10 19:52:52

标签: java generics classcastexception

作业内容如下:
使用普通队列数组提供优先级队列的完整实现。对于普通队列,请使用第402页的版本....

Pg402读取:

public class PriorityQueue<E>
{
    private ArrayQueue<E>[] queues;
    ...
     

在此实现中,构造函数使用以下语句为队列数组分配内存:

queues = (ArrayQueue<E>[]) new Object[highest+1];

但是:

  

线程中的异常&#34; main&#34; java.lang.ClassCastException:[Ljava.lang.Object;无法投射到[Lpriorityqueue.Queue;           at priorityqueue.PriorityQueue。(PriorityQueue.java:17)           at priorityqueue.PriorityQueue.main(PriorityQueue.java:67)       Java结果:1

data = (Queue<T>[]) new Object[highPriority];

上的例外情况
public class PriorityQueue<T>
{

    private Queue<T>[] data;
    private int size, hprior;
    @SuppressWarnings("unchecked")

    public PriorityQueue(int highPriority)
    {
        if(highPriority < 1)
            throw new RuntimeException("Invalid priority number!");

        data =  (Queue<T>[]) new Object[highPriority]; //Error line 17

        for(int i = 0; i < highPriority; i++)
        {
           data[i] = new Queue<>();
        }

    size = 0;
    }

    public void add(int priority, T element)
    {
        if(priority > data.length)
            throw new RuntimeException("Invalid priority number!");

        data[priority-1].enqueue(element);
        size++;
    }

    public T remove()
    {
        if(empty())
            throw new RuntimeException("Priority Queue is Empty!");

        T element = null;

        for(int i = data.length; i < 0; i--)
        {
          if(data[i].size()!=0)
              element = (T) data[i].dequeue();
          break;
        }

        return element;
    }

    public int size()
    {
        return size;
    }

    public boolean empty()
    {
        return size == 0;
    }

    public static void main(String[] args) 
    {
       PriorityQueue<String> pq = new PriorityQueue<>(10); //Error at line 67
       pq.add(1, "hi");
       pq.add(2, "there!");

       System.out.println(pq.remove());
    }


}

class Queue<T> 
{
    private int front, rear, size;
    public final static int DEFAULT_CAPACITY = 64;
    private T[] queue;

    public Queue(int capacity)
    {
        queue = (T[]) new Object[capacity];
        size = 0;
        front = 0;
        rear = 0;
    }

    public Queue()
    {
        this(DEFAULT_CAPACITY);
    }

    public void enqueue(T element)
    {
        if(size() == queue.length)
            throw new RuntimeException("Queue Full!");

        queue[rear]= element;
        rear = (rear +1) % queue.length;
        size++;
    }

    public T dequeue()
    {
        if(empty())
            throw new RuntimeException("Queue empty!");

        T element = queue[front];
        front = (front +1) % queue.length;
        size--;

        return element;
    }

    public int size()
    {
        return size;
    }

    public T front()
    {
        return queue[front];
    }

    public boolean empty()
    {
    return size == 0;
    }
}

1 个答案:

答案 0 :(得分:1)

您无法将Object随机投射到其他类型。如果您需要Queue<T>[],则需要实际构建一个。你不能真正创建一个带有泛型的数组,所以你将不得不做这样的事情:

Queue<T>[] queue = (Queue<T> []) new ArrayDeque[10]; //or whatever concrete implementation you want.