2014-05-10 9 views

回答

0

从这个例子中的PriorityVertex只是该提问者创建的类。这是图形中顶点的简单实现。

此类实现Comparable接口。原因是PriorityQueue必须比较其元素。这可以通过两种方式实现:

  • 元素必须要么实现Comparable接口
  • 一个Comparator将被传递到PriorityQueue,告诉队列怎样的元素进行比较。

所以PriorityVertex类可以大致如下所示:

class PriorityVertex implements Comparable<PriorityVertex> 
{ 
    private float priority; 

    // Setters, getters ... whatever the vertex needs 
    ... 

    // Implementation of the Comparable interface: 
    @Override 
    public int compareTo(PriorityVertex other) 
    { 
     return Float.compare(this.priority, other.priority); 
    } 
} 
相关问题