我正在使用 Java JungI图形包和 Netbeans 7。我从 Java 收到以下错误:
Exception in thread "main" java.lang.ClassCastException: graphvisualization.MyVertex cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
这是与错误相关的代码:
SortedMap<MyVertex, Double> vMap = new TreeMap<MyVertex, Double>();
double curRank = 0;
for(MyVertex v: g.getVertices()) //g is a SparseGraph<MyVertex, MyEdge>
{
curRank = vertexRank.getVertexScore(v);
vMap.put(v, curRank); //**Here is my Error**
}
MyVertex 类是我为图形制作的一个类。以下是 MyVertex 的代码
public class MyVertex
{
int vID; //id for this vertex
double centrality; //centrality measure for this vertex
int degree; //the degree of this vertex
public MyVertex(int id)
{
this.vID = id;
this.centrality=0;
this.degree=0;
}
public double getCentrality()
{
return this.centrality;
}
public void setCentrality(double centrality)
{
this.centrality = centrality;
}
public int getDegree()
{
return this.degree;
}
public void setDegree(int deg)
{
this.degree = deg;
}
public void incrementDegree()
{
this.degree++;
}
public void decrementDegree()
{
this.degree--;
}
@Override
public String toString()
{
return "v"+vID;
}
int compareTo(MyVertex v)
{
return (this.degree < v.degree) ? 1 : 0; //this will do descendingly
}
}
- 如何将 MyVertex 类型转换为 Comparables?
- 为什么这是必要的?(我没有立即看到原因)