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 › passing Array as Parameter
Page Index Toggle Pages: 1
passing Array as Parameter (Read 509 times)
passing Array as Parameter
Oct 23rd, 2006, 9:56am
 
I have an object with a lot of parameters, which I would like to group for clarity.

I had
   w1 = new walker(12,13,14,23,24,25,34,35,36,14,35,26);
calling
   walker(int a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3){...}

Now I have
   int [] a = {12,13,14};
   int [] b = {23,24,25};
   int [] c = {34,35,36};
   int [] d = {14,35,26};
   w1 = new walker(a,b,c,d);
calling
   walker(int [] aa, int [] bb, int [] cc, int [] dd){...}

But I would prefer simply
   w1 = new walker({12,13,14},{23,24,25},{34,35,36},{14,35,26});

But this last form does not work.
Compiler says:  unexpected token:{

Is there another way ?

------------------------------------------------
The actual line in my RandonWalker program is
 wc1 = new Walker(500,200,300,1,3,3,3,FF,TT,FF,TT,TT,TT,TT,192,128,64,64,0,0,0,0,128,64,64,
128,128,32,128,0,0,0,0,0,0,0,0,0,FF,FF,FF,FF);
I often manually change these parameters, and it's easy to change the wrong one.



Re: passing Array as Parameter
Reply #1 - Oct 23rd, 2006, 11:04am
 
You can use a syntax similar to:

Code:
w1 = new walker({12,13,14},{23,24,25},{34,35,36},{14,35,26}); 



But the proper syntax is (I think):
Code:
w1 = new walker(new int[]{12,13,14}, new int[]{23,24,25}, new int[]{34,35,36}, new int[]{14,35,26}); 

Re: passing Array as Parameter
Reply #2 - Oct 23rd, 2006, 11:49am
 
He, Thanks, I tried it and it work!

It is a bit cumbersome to write those "new int[]" and "new boolean[]", but that will do.

My object definition line now goes from:

 wc1 = new Walker(200,ww,hh,2,1,1,1,nrep,varep,TT,TT,TT,FF,FF,64,192,128,64,0,0,0,32,32,0,0
,0,0,32,0,0,0,0,32,0,16,16,16,0,TT,TT,TT,TT);

to (using Auto Format):

 wc1 = new Walker(200,ww,hh,2,1,1,1,nrep,varep,TT,TT,TT,FF,FF,new int[] {
   64,192,128,64  }
 ,new int[] {
   0,0,0,32  }
 ,new int[] {
   32,0,0,0  }
 ,new int[] {
   0,32,0,0  }
 ,new int[] {
   0,0,32,0  }
 ,new int[] {
   16,16,16,0  }
 ,new boolean[] {
   TT,TT,TT,TT  }
 );

Is it better now? I'm not sure.

By the way, I always define TT=true and FF=false.
Re: passing Array as Parameter
Reply #3 - Oct 23rd, 2006, 12:06pm
 
To be honest, I think you might have more luck in adding clarity, if you don't pass every single parameter into the constructor. If there are groups, create a seperate method for setting those paramters, e.g.

Code:
class walker
{
int .... // all your variables
walker(){}
void setFoo(int a, int b, int c, int d)
{
//apply these numbers.
}
void setBar(..)
{
//etc...
}
// etc, etc, etc...
}
Re: passing Array as Parameter
Reply #4 - Oct 23rd, 2006, 9:33pm
 
It's a good idea.

This way, one thing I could do is set default values into the constructor,
and change using other methods only when needed.

So for example

w = new walker(aa,bb,x1,x2,x3,x4,y1,y2,y3,y4,colx1,colx2,coly1,coly2);

could become

w = new walker(aa,bb);
set_coord(x1,x2,x3,x4,y1,y2,y3,y4);
set_colors(c1,c2,c3,c4);
Page Index Toggle Pages: 1