0

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.

  • Arrays and generics don't mix well. You can use `List>>`, for instance with `ArrayLists`. – Socowi May 07 '17 at 10:30
  • Are you sure you don't need a PriorityQueue of PriorityQueues? – user207421 May 07 '17 at 10:34
  • Yes, I want an array because I want to create a Queue for each vertex. I have 300 vertexes and for each one i want to create a queue with the distances to the rest vertexes. Making a priorityQueue of PriorityQueues is just complicating the code i think For each vertex (the index of the array) I will have an const access to the queue equal 1. – Dawid Dave Kosiński May 07 '17 at 10:40

1 Answers1

3

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.

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20