2

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.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
BroLegend
  • 59
  • 6
  • Do you have any other errors? `PriorityQueue` is implementing `Iterator` but I don't see the implemented `Iterator` methods. – Jonny Henly Apr 13 '16 at 02:10
  • @JonnyHenly I didnt paste the rest of class PriorityQueue to save space, I have implemented Iterator. – BroLegend Apr 13 '16 at 02:12
  • @JonnyHenly of course... – BroLegend Apr 13 '16 at 02:22
  • 1
    In your IDE, try doing a clean & rebuild of your project. Clean & rebuild sometimes fixes things like this. – Radiodef Apr 13 '16 at 02:29
  • @Radiodef Tried, not working, I use Intellij – BroLegend Apr 13 '16 at 02:36
  • I don't use IntelliJ so I can't provide any extra advice if this is an IDE issue. I know I've had a sticky problem like this with Netbeans where clean & rebuild didn't work. Maybe there is some IntelliJ-specific advice out there. Of course it could also be a problem with imports etc. Make sure you don't have e.g. 2 classes called `QueueItem` and the wrong one is imported. – Radiodef Apr 13 '16 at 02:46

1 Answers1

2

OH. It was staring me in the face.

public class PriorityQueue<QueueItem> implements Iterator<QueueItem> {
//                        ^^^^^^^^^^^

You're declaring a type variable here with the same name as the class QueueItem which shadows it.

So I think you just want to remove that:

public class PriorityQueue implements Iterator<QueueItem> {

If you intended for PriorityQueue to be generic then I'm not sure exactly what you need to do to fix it. Perhaps you would want something like this:

public class PriorityQueue<E> implements Iterator<QueueItem<E>> {
    ...
    private Map<Integer, List<QueueItem<E>>> pq;
    ...
}

public class QueueItem<E> implements Comparable<QueueItem<E>> {
    ...
    private E value;
    ...
}

As a side note, using the raw type Comparable isn't good. Even with the non-generic class QueueItem you should have implements Comparable<QueueItem>.

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • 1
    Wow you're right, I just copy pasted OP's code into Eclipse and it gave me a warning with exactly what you said. +1 – Jonny Henly Apr 13 '16 at 03:10