title: Why am I getting a Class Cast Exception?
body: <p>The assignment reads:<br>
  Give a complete implementation of a priority queue using an array of ordinary queues.  For your ordinary queue, use the version...on page 402.</p>
  
  <p>Pg402 reads:</p>
  
  <blockquote>
  <pre><code>public class PriorityQueue&lt;E&gt;
  {
      private ArrayQueue&lt;E&gt;[] queues;
      ...
  </code></pre>
    
    <p>In this implementation, the constructor allocates the memory for the array of queues with the statement:  </p>
  
  <pre><code>queues = (ArrayQueue&lt;E&gt;[]) new Object[highest+1];
  </code></pre>
  </blockquote>
  
  <p>However:  </p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>Exception on <code>data = (Queue&lt;T&gt;[]) new Object[highPriority];</code>  </p>
  
  <pre><code>public class PriorityQueue&lt;T&gt;
  {
  
      private Queue&lt;T&gt;[] data;
      private int size, hprior;
      @SuppressWarnings("unchecked")
  
      public PriorityQueue(int highPriority)
      {
          if(highPriority &lt; 1)
              throw new RuntimeException("Invalid priority number!");
  
          data =  (Queue&lt;T&gt;[]) new Object[highPriority]; //Error line 17
  
          for(int i = 0; i &lt; highPriority; i++)
          {
             data[i] = new Queue&lt;&gt;();
          }
  
      size = 0;
      }
  
      public void add(int priority, T element)
      {
          if(priority &gt; 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 &lt; 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&lt;String&gt; pq = new PriorityQueue&lt;&gt;(10); //Error at line 67
         pq.add(1, "hi");
         pq.add(2, "there!");
  
         System.out.println(pq.remove());
      }
  
  
  }
  
  class Queue&lt;T&gt; 
  {
      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;
      }
  }
  </code></pre>