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
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;
}
}
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