【问题标题】:Bound Mismatch error when trying to implement comparable interface on generic class尝试在泛型类上实现可比较的接口时出现绑定不匹配错误
【发布时间】:2013-03-08 04:34:28
【问题描述】:

我正在为家庭作业创建自定义 PriorityQueue 类,但遇到了障碍。我不知道如何解决我在 Eclipse 中遇到的这个 Bound Mismatch 错误;它不会让我按照它所说的那样尝试编译:

边界不匹配:K 类型不是 Entry<K,V> 类型的有界参数 <K extends Comparable<K>> 的有效替代品

PriorityQueue.java:

  public class PriorityQueue<K,V> {

    private Entry<K,V> _head;
    private Entry<K,V> _tail;
    private int _size;

    public PriorityQueue() {
      this._head = null;
      this._tail = null;
      this._size = 0;
    }

    public int size() {
      return _size;
    }

    public boolean isEmpty() {
      return (size() == 0);
    }

    public Entry<K,V> min() {
      if (_head == null) {
        return null;
      }
      Entry<K,V> current = _head;
      Entry<K,V> min = _head;;

      while (current != null) {
        if (current.compareTo(min) < 0) {
          min = current;
        }
        current = current.getNext();
      }
      return min;
    }

    public Entry<K,V> insert(K k, V x) {
      Entry<K,V> temp = new Entry<K,V>(k,x);
      if (_tail == null) {
        _tail = temp;
        _head = temp;
      }
      else {
        _tail.setNext(temp);
        temp.setPrev(_tail);
        _tail = temp;
      }
      return temp;
    }

    public Entry<K,V> removeMin() {
      Entry<K,V> smallest = min();
      smallest.getPrev().setNext(smallest.getNext());
      smallest.getNext().setPrev(smallest.getPrev());

      return smallest;
    }

    public String toString() {
      return null;
    }

    public static void main(String[] args) {
      PriorityQueue<Integer, Integer> pq = 
          new PriorityQueue<Integer, Integer>();

      pq.insert(4, 2);
      pq.insert(5, 1);
      pq.insert(1, 5);

      System.out.println(pq.min().toString());
    }
  }

Entry.java:

public class Entry<K extends Comparable<K>,V> implements Comparable<Entry<K,V>> {

  private V _value;
  private K _key;
  private Entry<K,V> _prev;
  private Entry<K,V> _next;

  public Entry(K key, V value) {
    this._value = value;
    this._key = key;
    this._prev = null;
    this._next = null;
  }

  public V getValue() {
    return this._value;
  }

  public K getKey() {
    return this._key;
  }

  public Entry<K,V> getNext() {
    return _next;
  }

  public void setNext(Entry<K,V> link) {
    this._next = link;
  }

  public Entry<K,V> getPrev() {
    return _prev;
  }

  public void setPrev(Entry<K,V> link) {
    this._prev = link;
  }

  public String toString() {
    return "" + this.getValue();
  }

  @Override
  public int compareTo(Entry<K,V> o) {
    if (o instanceof Entry<?,?>) {
      return this.getKey().compareTo(o.getKey());
    }
    throw new IllegalArgumentException("Objects are not comparable.");
  }


}

【问题讨论】:

  • 为获得最佳效果,请使用K extends Comparable&lt;? super K&gt;

标签: java generics


【解决方案1】:

对于PriorityQueue,您也必须使 K 扩展 Comparable

public class PriorityQueue<K extends Comparable<K>,V> {

确实,泛型类型 K 将用于Entry&lt;K,V&gt;,并且 Entry 具有必须扩展(实际上实现)Comparable&lt;K&gt; 的约束。您必须告诉编译器,PriorityQueue 的 K 也满足此约束,因此它允许您使用 Entry&lt;K,V&gt;

如果没有,有人可以将 PriorityQueue 与不会扩展 Comparable 的 K 一起使用,编译器必须检查 K 的所有使用情况,以查看是否没有阻止它使用的约束。

【讨论】:

  • 啊;从来没有见过。感谢您的意见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 2014-06-26
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
相关资源
最近更新 更多