adding a node to a collection using JUNG2

百般思念 提交于 2019-12-12 04:09:59

问题


I am trying to add a node like this ( C.add(n)))

I have this problem:

Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Unknown Source))

Non-executable code example:

UndirectedSparseMultigraph<MyNode, MyLink> g = getgraph1();
Collection<MyNode> c = null ;
for( MyNode n : g.getVertices() ){
  if( n.id == 3 ){
    c = g.getNeighbors(n);
    System.out.println(C); C.add(n); }
}

回答1:


You are trying to use UndirectedSparseMultigraph.getNeighbors(V vertex) to get the Vertices this method returns an unmodifiable collection

  public Collection<V> getNeighbors(V vertex) {
  ...
    return Collections.unmodifiableCollection(neighbors);
  }

As do

  public Collection<V> getVertices()
  {
    return Collections.unmodifiableCollection(vertex_maps.keySet());
  }

and

  public Collection<E> getEdges()
  {
    Collection<E> edges = new ArrayList<E>(directed_edges.keySet());
    edges.addAll(undirected_edges.keySet());
    return Collections.unmodifiableCollection(edges);
  }

Based on your comments it appers that you are trying to add a node n to the collection of its neighbors. If this is the case have your tried replacing

( C.add(n)))

with

g.addEdge(new MyLink(), n, n);

to add a self intersection.



来源:https://stackoverflow.com/questions/12069243/adding-a-node-to-a-collection-using-jung2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!