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 & HelpSyntax Questions › How to define multiple variables with same value
Page Index Toggle Pages: 1
How to define multiple variables with same value? (Read 1037 times)
How to define multiple variables with same value?
May 2nd, 2010, 10:46pm
 
Hi,

I was wondering if anyone knows of a way to define multiple variables with the same value?

A shorthand version of this:

int di1 = 50;
int di2 = 50;
int di3 = 50;
int di4 = 50;
int di5 = 50;
int di6 = 50;
int di7 = 50;

Cheers,

Help is much appreciated  Smiley
Re: How to define multiple variables with same value?
Reply #1 - May 2nd, 2010, 11:03pm
 
You really should use an array.

http://processing.org/reference/Array.html
Re: How to define multiple variables with same value?
Reply #2 - May 2nd, 2010, 11:35pm
 
Understood, but how do I do this with an array? I'm not sure I understand arrays fully.

Thanks for your response.
Re: How to define multiple variables with same value?
Reply #3 - May 3rd, 2010, 1:26am
 
use looping:

int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
 arr[i] = 12345;
}
Re: How to define multiple variables with same value?
Reply #4 - May 3rd, 2010, 1:32am
 
When you go shopping, you don't write each separate item you want to buy on a separate piece of paper. Instead, you make a list of things to buy on a single sheet of paper. This is how arrays work too. Instead of having a name for each variable you have, you just have a list of variables. For example, instead of saying:

String firstThingToBuy = "apples";
String secondThingToBuy = "milk";
String thirdThingToBuy = "bananas";

You can just do:

String [] thingsToBuy = { "apples", "milk", "bananas" };

And then read them later with:

println( thingsToBuy[1] ); // prints "milk"
Re: How to define multiple variables with same value?
Reply #5 - May 3rd, 2010, 2:28am
 
"lie Ryan", thanks very much for your example. And thankyou for your explaination TfGuy44. I knew the concept but not that you could make every item in the array a single value.

Cheers.
Page Index Toggle Pages: 1