问题
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