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
- How can I iterate through my set of vertices?
- 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;
}
}
g2? It looks like you're not using generics correctly andgetVertices()is returning aCollection<Object>which is why theforloop won't work. - Brian Roach