Question

I am trying to iterate through a set of vertices. The vertices are a custom class that I created. Here is my attempt to iterate through the vertices:

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

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

The error I get is from the line: MyVertex v : g2.getVertices() and the message is:

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

So, I tried casting to an ArraryList<MyVertex> and I got this error message:

Exception in thread "main" java.lang.ClassCastException: java.util.Collections$UnmodifiableCollection cannot be cast to java.util.ArrayList
  1. How can I iterate through my set of vertices?
  2. The ultimate goal is to set the centrality of each vertex

The following is my code for the MyVertex Class:

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;
    }
}
Was it helpful?

Solution

I am guessing g2.getVertices() returns a collection. so you can convert your Collection to ArrayList as:

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

Here is the documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top