I created a priority queue which contains QueueItem
objects. But even though I have already implemented getPriority()
in the QueueItem
class, it still says cannot resolve method getPriority()
in the method insert()
of the PriorityQueue
class.
Here is my PriorityQueue
class:
public class PriorityQueue<QueueItem> implements Iterator<QueueItem> {
private int maxSize;
private int size;
private Map<Integer, List<QueueItem>> pq;
public PriorityQueue(int maxSize) {
if (maxSize < 0) throw new IllegalArgumentException();
this.maxSize = maxSize;
pq = new HashMap<Integer, List<QueueItem>>();
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void insert (QueueItem item) {
int priority = item.getPriority(); //here is where the problem occured
// pq.get(priority)
}
}
Here is my QueueItem
class:
public class QueueItem implements Comparable{
private int priority;
private Object value;
public QueueItem() {
priority = -1;
value = null;
}
public QueueItem(int priority, Object value) {
this.priority = priority;
this.value = value;
}
public int getPriority() {
return priority;
}
public Object getValue() {
return value;
}
public int compareTo(Object o) {
if (!(o instanceof QueueItem)) throw new ClassCastException();
if (((QueueItem)o).getPriority() == -1) throw new NullPointerException();
return priority - ((QueueItem) o).getPriority();
}
}
As you can see, the method getPriority()
simply returns an integer priority.
I appreciate in advance if anyone could point out the error I have made. Thanks.