GraphStream Users

Archives de la liste Aide

Re: For ( Node n : graph ) performing exactly ( graph.getNodeCount()/2 ) +1 iterations

Chronologique Discussions 
  • From: Stefan Balev <stefan.balev AT>
  • To: graphstream-users AT, Rajesh Veeranki <rveeranki01 AT>
  • Subject: Re: For ( Node n : graph ) performing exactly ( graph.getNodeCount()/2 ) +1 iterations
  • Date: Sun, 1 Jul 2012 01:55:17 +0200

Hi Raj,

Your problem is that you remove nodes in your for loop. This loop uses implicitly a node iterator and iterators in GraphStream follow the same rules as iterators in JCF:
In particular this means that the only allowed way to modify your collection during an iteration is to use the remove() method of the iterator.

You will have this kind of problem even with JCF collections. A loop like this:

Collection<Animal> animals = new ArrayList<Animal>();
for (Animal animal : animals) {
  if (animal.isFurry())

will most likely throw an exception.

The right way to iterate and remove is the following:

Iterator<Node> it = g.getNodeIterator();
while (it.hasNext()) {
  Node node =;
  if (condition on node)

By the way, your list is typically smaller than the number of the nodes and it is much more efficient to do it the other way around:

for (String id : l)



2012/7/1 Rajesh Veeranki <rveeranki01 AT>:
> Hi all,
> I ran into some problem with the for loop i.e; 
> for ( Node n : graph ) seems to perform half  the number of iterations of
> the number of nodes graph has.
> I think it is expected to perform iterations = number of nodes.
> I'm afraid i may be foolish here.
> I'm giving a link to the code
> Please help.
> Thanks,
> Rajesh.

Archives gérées par MHonArc 2.6.16.

Top of page