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.
Page Index Toggle Pages: 1
Array (Read 850 times)
Array
Mar 21st, 2010, 7:25am
 
What's an array? I don't get it. Huh
Re: Array
Reply #1 - Mar 21st, 2010, 7:54am
 
Have you read this:

http://processing.org/reference/Array.html

An array is just a list of things - variables or objects. For example if you had 1000 height measurements you could store them in an array of floating point numbers.

float[] heights = new float[1000];

means make me an array of floats, length 1000. Once you have put the values in the array you can then refer to them by the index number (which ranges from 0 to 999).

heights[0] is the first value. heights[56] is the 57th value....and so on.
Re: Array
Reply #2 - Mar 21st, 2010, 11:38am
 
I read it in the reference, but didn't understand it. Can you apply it in an example?
Re: Array
Reply #3 - Mar 21st, 2010, 11:48am
 
Yep, here's a sketch I wrote which uses arrays to hold the positions and speeds of the particles:

http://www.openprocessing.org/visuals/?visualID=6407
Re: Array
Reply #4 - Mar 22nd, 2010, 12:18pm
 
INCREDIBLE!

Question:
What's the difference between:
int[] numbers = new int[3];
AND
int x; int y; int z;
Re: Array
Reply #5 - Mar 22nd, 2010, 12:57pm
 
In effect, nothing. But in the case of 1000 of them you would have to use 1000 lines defining them all, and then think of 1000 different names. And then when you wanted to do something to all 1000 of them... again, another 1000 lines of code.....

Or, using an array, to multiply all numbers by 2:

for(int i=0; i<1000; i++)
{
   numbers[i] = numbers[i] * 2;
}
Page Index Toggle Pages: 1