Search code examples
javaarrayspriority-queue

How to create a array of PriorityQueues


I'm writing an algorithm which needs an array of PriorityQueues. Can I get something like:

public double[][] distance_table = new double[300][300];

Using priorityQueue? I tried:

public PriorityQueue<Double>[] queue_table = new PriorityQueue<>(300, comparator)[300];

But Netbeans says:

array required, but PriorityQueue found.

It has an error icon so it isn't a warning.


Solution

  • You can't create arrays of class type as you have shown : In your case you can do the following :

    PriorityQueue<Double> queue_table[] = new PriorityQueue[10];
    for(int i=0;i<queue_table.length;i++){
       queue_table[i] = new PriorityQueue<>(300, comparator);            
    }
    

    Here we are first declaring an array of type PriorityQueue, not still initializing it. Then we have used loop for initializing it's elements.