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 › nested array problem
Page Index Toggle Pages: 1
nested array problem (Read 435 times)
nested array problem
Jan 5th, 2009, 7:11pm
 
check this out:



float x = 100;
float y = 100;
float rotation;
float friction = -0.7;
boolean alive = false;

float cubesize = 50;

float[] line1points = {1,2,3,4};
float[] line2points = {1,2,3,4};
float[] line3points = {1,2,3,4};
float[] line4points = {1,2,3,4};


float lines[][] = new float[4][];

lines[0] = line1points;
lines[1] = line2points;
lines[2] = line3points;
lines[3] = line4points;

println(lines[0]);
println(lines[1]);
println(lines[2]);
println(lines[3]);



this code works, it's a little test in getting an array in another array. But the problem is this: as soon as I move this code into a class so it looks like this:

class Bouncer {

float x = 100;
float y = 100;
float rotation;
float friction = -0.7;
boolean alive = false;

float cubesize = 50;


float[] line1points = {1,2,3,4};
float[] line2points = {1,2,3,4};
float[] line3points = {1,2,3,4};
float[] line4points = {1,2,3,4};


float lines[][] = new float[4][];

lines[0] = line1points;
lines[1] = line2points;
lines[2] = line3points;
lines[3] = line4points;

println(lines[0]);
println(lines[1]);
println(lines[2]);
println(lines[3]);

... (methods after this...)



it says: Syntax error, maybe a missing ] character?
at this line: lines[0] = line1points;

even if I comment all of the other code in the class and just keep this code, it gives that error...

any help with this one? i'm clueless...
Re: nested array problem
Reply #1 - Jan 5th, 2009, 9:51pm
 
Remove the println calls and it should work.

You are allowed to define methods and variables inside the main class block, but calling methods from there is a no no.
Re: nested array problem
Reply #2 - Jan 6th, 2009, 9:44am
 
Hi,

the problem are not only the println(), but the statements outside an enclosing method.

float lines[][] = new float[4][];

is ok, because it is a variable declaration with initialization.

lines[0] = line1points;

is NOT ok, it is a statement which may not be used outside a method.

you can initialize your array this way:
float lines[][] = {
 {1,2,3,4},
 {1,2,3,4},
 {1,2,3,4}
};

Ilu
Re: nested array problem
Reply #3 - Jan 7th, 2009, 8:46am
 
Alright thanks alot! I'll remember it for the rest of my days Smiley

Bit of a silly mistake, if you look at it this way...
Page Index Toggle Pages: 1