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 › Multidimentional Array
Page Index Toggle Pages: 1
Multidimentional Array (Read 617 times)
Multidimentional Array
Jan 19th, 2008, 3:20pm
 
Hy everybody,

I am learning to use arrays on processing, and I found out some array functions like subset, concat and the .lenght syntax.

I've started to use multidimentional arrays and I realised that that syntax doesn't work... So maybe I'm using it in a wrong way... Or maybe we can't use it at all... so...

How can we use the .length and the subset function for multydimentional arrays?

And how do I use it for a dimention diferent then the first array dimention?

Code exemple :

int [][] array1 = new int [10][20];
int [][] array2 = new int [10][20];
int k=0;
for(int i = 0; i<10; i++)
{
 for(int j = 0; j<20; j++)
 {
   array1[i][j]=k;
   array2[i][j]=200+k;
   k++;
 }
}
int [][] array3 = (int[][])concat(array1, array2);

for(int i = 0; i<20; i++)
{
 for(int j = 0; j<20; j++)
 {
   println(array3[i][j]);
 }
}

int [][] array4 = (int[][])subset(array3, 2, 5);

for(int i = 0; i<5; i++)
{
 for(int j = 0; j<20; j++)
 {
   println(array4[i][j]);
 }
}

println(array4.length);

In this case, the println(array4.length); returns the first dimention lenght. How can I return the second the second dimention lenght?

What functions can we use for multidimentional arrays and how can we use theme? (no reference on processing website)...
Re: Multidimentional Array
Reply #1 - Jan 19th, 2008, 8:06pm
 
As the arrays in the second dimension could have various sizes (maybe not in your case) you need to check the size of Array at position n like this: array4[n].length
Re: Multidimentional Array
Reply #2 - Jan 20th, 2008, 2:19pm
 
thanks, but this syntax only works for the last dimension...I can't know the length of the first dimension for instance...

example:

int [][][][] array = new int[10][20][30][40]; // let's name the dimensions to get it clearer... here we declare an int array variable with 4 dimensions, from left to right : dim1, dim2, dim3, dim4

println(array[0][0][0].length)// this will return the length of dim4 that means 40

my question is if we don't know the length of dim1, dim2 or dim3 how can we get the length value?

Is there a syntax like array.length for dim1, array[].length for dim2, array[][].length for dim3 and array[][][].length for dim4?

And that makes me think on something...


if we declare an array with two or more dimensions, are we forced to have the same length for the same dimension level (I call dimension level the dim1, dim2, dim3... thing)?


For instance :

int [][][][] array = new int[10][20][30][40]; //here we declare an int array variable with 4 dimensions, from left to right : dim1, dim2, dim3, dim4

println(array[0][0][0].length)// this will return the length of dim4 that means 40

but can we have this situation :

int [][][][] array = new int[10][20][30][40];
array [0][0][1] = shorten(array);
println(array[0][0][0].length)//returns 40
println(array[0][0][1].length)//returns 39?

lots of questions...
Re: Multidimentional Array
Reply #3 - Jan 21st, 2008, 6:40pm
 
I'm trying to wrap my head around it, but I don't know why you would want to use a four-dimensional array. It seems like a painful way to manage information. I think you could be storing things like [object][x][y][z] in that manner?

If you want to store sets of related data, it might be easier to create objects specifically to hold that data. That way, it is also easy to add more types of information to each object (we're not limited to huge chains of integers).

zum beispiel:

ValueHolder3d[] positions;

void setup(){
 size(1024,768, P3D);
 positions = new ValueHolder3d[100];
 for( int i = 0; i < positions.length; i++ ){
   positions[i] = new ValueHolder3d( random(-1000,1000), random(-1000, 1000), random(-1000) );
 }
}

void draw(){
 background(0);
 translate(width/2, height/2, 0);
 
 for( int i=0; i < positions.length; i++ ){
   //get a shorter reference to the content in the array
   ValueHolder3d v = positions[i];
   pushMatrix();
   translate( v.x, v.y, v.z);
   ellipse( 0, 0, 50, 50 );
   popMatrix();
   //add some jitter to the circles
   v.x += random(-1,1);
   v.y += random(-1,1);
   v.z += random(-1,1);
 }
}


//a simple class that holds values for us
class ValueHolder3d{
 float x;
 float y;
 float z;

 ValueHolder3d( float _x, float _y, float _z ){
   x = _x;
   y = _y;
   z = _z;
 }
}
Page Index Toggle Pages: 1