Why am I getting a Class Cast Exception?

1.3k views Asked by At

The assignment reads:
Give a complete implementation of a priority queue using an array of ordinary queues. For your ordinary queue, use the version...on page 402.

Pg402 reads:

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

In this implementation, the constructor allocates the memory for the array of queues with the statement:

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

However:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lpriorityqueue.Queue; at priorityqueue.PriorityQueue.(PriorityQueue.java:17) at priorityqueue.PriorityQueue.main(PriorityQueue.java:67) Java Result: 1

Exception on 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

There are 1 answers

0
Vivin Paliath On

You can't randomly cast Object to some other type. If you want a Queue<T>[], you need to actually construct one. You can't really create an array with generics, so you're going to have to do something like this:

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