1

我正在尝试遍历一组顶点。顶点是我创建的自定义类。这是我尝试遍历顶点的尝试:

bCentral2 = new BetweennessCentrality<MyVertex, MyEdge>(g2);

for(MyVertex v : g2.getVertices())
{
    v.setCentrality(bCentral2.getVertexScore(v));
}

我得到的错误来自该行:MyVertex v : g2.getVertices()并且消息是:

incompatible types
  required: graphvisualization.MyVertex
  found:    java.lang.Object 

因此,我尝试转换为 anArraryList<MyVertex>并收到此错误消息:

Exception in thread "main" java.lang.ClassCastException: java.util.Collections$UnmodifiableCollection cannot be cast to java.util.ArrayList
  1. 如何遍历我的一组顶点?
  2. 最终目标是设置每个顶点的中心性

以下是我的 MyVertex 类代码:

public class MyVertex 
{
    int vID;                    //id for this vertex
    double centrality;          //centrality measure for this vertex

    public MyVertex(int id)
    {
        this.vID = id;
        this.centrality=0;
    }

    public double getCentrality()
    {
        return this.centrality;
    }

    public void setCentrality(double centrality)
    {
        this.centrality = centrality;
    }

    public String toString()
    {
        return "v"+vID;
    }
}
4

1 回答 1

1

我猜g2.getVertices()返回一个集合。所以你可以将你Collection的转换ArrayList为:

ArrayList<MyVertex> ll = new ArrayList<>(g2.getVertices())

这是文档

于 2012-12-31T06:08:36.680 回答