We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › understandig some code
Page Index Toggle Pages: 1
understandig some code (Read 700 times)
understandig some code
Aug 23rd, 2009, 11:25am
 
Hello, im trying to understand some code i found, there are 2 new things ive never seen in processing . Can anybody explain me what does it does?

the first one is this:
Set dotsNearMouse = hashGrid.get(new PVector(mouseX,mouseY));

what does set does? i why does the variable dotsNearMouse is not declared in the code?

and my last question is what does this means:  for (Iterator i=dotsNearMouse.iterator(); i.hasNext()Wink {}

?


here is the code:



http://www.soi.city.ac.uk/~jwo/processing/hashGrid/index.html

thanks
Re: understandig some code
Reply #1 - Aug 23rd, 2009, 12:03pm
 
I've never used Set, but the code seems fairly well documented on the page you linked to.  From the linked documentation Set is clearly a class, so the line quoted is the declaration of dotsNearMouse...

iterator is a method of Set and seems fairly self-explanatory...  Looks like it's needed in order to iterate through the contents of a Set, something like using a for loop to iterate over the contents of an array.

To be fair the styling on the site makes the links to documentation a little obscure, but they're there all right...
Re: understandig some code
Reply #2 - Aug 23rd, 2009, 1:38pm
 
Indeed, Set is a class. It's a Java Collection which, as the Javadoc states, contains no duplicate elements. For example : a set of cards.

Iterators are useful to... iterate through a collection. I won't go into details (see the link below) but for many reasons, you won't use this :
Code:
for (int i = 0; i < mySet.size(); i++) {
 myObject = mySet.get(i);
}


but this instead :
Code:
for (Iterator i = mySet.iterator(); i.hasNext(); ) {
 myObject = i.next();
}


See this Java tutorial about Collections for more info.
Page Index Toggle Pages: 1