GraphStream Users

Archives de la liste Aide


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


Chronologique Discussions 
  • From: Rajesh Veeranki <rveeranki01 AT gmail.com>
  • To: Stefan Balev <stefan.balev AT gmail.com>
  • Cc: graphstream-users AT litislab.fr
  • Subject: Re: For ( Node n : graph ) performing exactly ( graph.getNodeCount()/2 ) +1 iterations
  • Date: Sun, 1 Jul 2012 05:59:06 +0530

Hi Stefan,

Perfect ! It worked now. I didn't know that!

Thanks,
Rajesh V

On Sun, Jul 1, 2012 at 5:25 AM, Stefan Balev <stefan.balev AT gmail.com> wrote:
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:
http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html#remove()
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())
    animals.remove(animal);
}

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 = it.next();
  if (condition on node)
    it.remove();
}

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)
  g.removeNode(id);

Best,

--
Stefan

2012/7/1 Rajesh Veeranki <rveeranki01 AT gmail.com>:

> 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