Java-Jung不兼容类型转换

Java-Jung不兼容类型转换,java,casting,jung,vertex,Java,Casting,Jung,Vertex,我试图通过一组顶点进行迭代。顶点是我创建的自定义类。以下是我尝试遍历顶点的步骤: bCentral2 = new BetweennessCentrality<MyVertex, MyEdge>(g2); for(MyVertex v : g2.getVertices()) { v.setCentrality(bCentral2.getVertexScore(v)); } 因此,我尝试强制转换到阵列列表,并收到以下错误消息: Exception in thread "mai

我试图通过一组顶点进行迭代。顶点是我创建的自定义类。以下是我尝试遍历顶点的步骤:

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

for(MyVertex v : g2.getVertices())
{
    v.setCentrality(bCentral2.getVertexScore(v));
}
因此,我尝试强制转换到
阵列列表
,并收到以下错误消息:

Exception in thread "main" java.lang.ClassCastException: java.util.Collections$UnmodifiableCollection cannot be cast to java.util.ArrayList
  • 如何迭代我的顶点集
  • 最终目标是设置每个顶点的中心
  • 以下是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;
        }
    }
    

    我猜
    g2.getVertices()
    返回一个集合。因此,您可以将
    集合
    转换为
    阵列列表
    ,如下所示:

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

    下面是我猜的g2.getVertices()返回一个集合。因此,您可以将
    集合
    转换为
    阵列列表
    ,如下所示:

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

    这是

    你能展示一下你是如何实例化g2的吗?看起来您没有正确使用泛型,
    getVertices()
    正在返回一个
    集合
    ,这就是为什么
    for
    循环无法工作的原因。您能否演示一下如何实例化
    g2
    ?看起来您没有正确使用泛型,并且
    getVertices()
    正在返回一个
    Collection
    ,这就是为什么
    for
    循环不起作用的原因。这不起作用。他的
    for
    循环之所以失败,首先是因为它是一个
    集合,正如他收到的“不兼容类型”错误所指出的那样。这不起作用。首先,他的
    for
    循环失败的原因是,正如他收到的“不兼容类型”错误所指出的,这是一个
    集合。