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 › Extend a 2D array
Page Index Toggle Pages: 1
Extend a 2D array (Read 781 times)
Extend a 2D array
Nov 18th, 2007, 5:47am
 
How can I extend a 2D array? I've try this :

String[][] LSystem2D = new String[7][11];

LSystem2D = (String[][]) expand(LSystem2D, LSystem2D.length+1);
LSystem2D = (String[][]) expand(LSystem2D, LSystem2D[0].length+1);
println(LSystem2D.length); // 12
println(LSystem2D[0].length); // 11

Look like it's working for the second parameter(array) but not for the first. What do I missunderstand? (or not understand at all)

Also, what if I would like to append a value instead? Thx a lot!
Re: Extend a 2D array
Reply #1 - Nov 18th, 2007, 9:27am
 
For the expand, this will help (look at Fry's post):
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1193701437;start=5#5

To append an array, you need a cast.
Here's what you need to do (code by JohnG):

Code:

int[][] test = {{1,2},{3,4}};
test = (int[][])append(test, new int[]{5,6});
println(test.length);


Code from -> http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1193515933
Re: Extend a 2D array
Reply #2 - Nov 18th, 2007, 4:30pm
 
Look ok for a simple array but don't work for a 2D array.. Sad
Re: Extend a 2D array
Reply #3 - Nov 18th, 2007, 6:05pm
 
Sorry, I misread your post.

Your code for expanding arrays worked.
I believe that there is just a little logic problem here:

Quote:
LSystem2D = (String[][]) expand(LSystem2D, LSystem2D[0].length+1);


You're expanding the outer array (LSystem2D) to the size of the inner array (LSystem2D[0]).
So your outer array becomes (size of LSystem2D[0]) 11 + 1 which equals 12. So the code works.

I believe you want to expand the inner array. To do that, you need to specify the array you want to expand.
Code:

LSystem2D[0] = (String[]) expand(LSystem2D[0], LSystem2D[0].length+1);


Take a look at the code below:

Code:

String[][] LSystem2D = new String[7][11];

println(LSystem2D.length); // 7
LSystem2D = (String[][]) expand(LSystem2D, LSystem2D.length + 1);
println(LSystem2D.length); // 8

println(LSystem2D[0].length); // 11
LSystem2D[0] = (String[]) expand(LSystem2D[0], LSystem2D[0].length+1);
println(LSystem2D[0].length); // 12
Re: Extend a 2D array
Reply #4 - Nov 18th, 2007, 7:17pm
 
Thx a lot dude! It was a problem with the typecasting.
Page Index Toggle Pages: 1