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 › Silly me, how to do this Create 2-d object arrays
Page Index Toggle Pages: 1
Silly me, how to do this? Create 2-d object arrays (Read 443 times)
Silly me, how to do this? Create 2-d object arrays
Dec 7th, 2009, 9:08am
 
I'm having trouble creating a 2-D array of objects. I know 1-D
object Array1[]={};
Array1=(object)append(Array1,new object(init));

How do I do it in 2-D?
Two cases, say if I know the dimension is Aray2[4][3] in case one, and say if I don't know the dimension till at runtime in case two? Thanks a lot.
Re: Silly me, how to do this? Create 2-d object arrays
Reply #1 - Dec 7th, 2009, 9:19am
 
I would just avoid using "dynamic" 2D arrays... You need them to do what?

In Java, 2D arrays are actually arrays of arrays, meaning you can actually have jagged arrays (different length on each row).
Example:
Code:
int[][] ar = new int[5][];
ar[0] = new int[1];
ar[1] = new int[2];
ar[2] = new int[3];
ar[3] = new int[4];
ar[4] = new int[5];
ar[2][2] = 42;

println(ar[2]);

int[][] arr = new int[4][2];
arr[2][1] = 111;
println(arr[2]);
Page Index Toggle Pages: 1