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 › arrays from objects to array
Page Index Toggle Pages: 1
arrays from objects to array (Read 806 times)
arrays from objects to array
Jul 30th, 2009, 6:00am
 
Hello , i have an array of objects each object has an array with 4 float numbers in it.  I need to copy the values from each one of the arrays that is contained in each object into a new array, for example :
if i have 3 objects each one with one array:

object one - [1,2,3,4 ]
object two - [3,2,3,2]
object three - [3,3,3,3]

i would like to copy all these arrays into one array like this:

[1 , 2, 3, 4 ,3, 2, 3, 2 , 3, 3, 3 ,3]

Any idea of how can i do this?


thanks
Re: arrays from objects to array
Reply #1 - Jul 30th, 2009, 6:17am
 
Looks like concat should do the trick, though you'll obviously have to create a new array and then concatenate each array to this in turn.

You might also want to look at ArrayList.
Re: arrays from objects to array
Reply #2 - Jul 30th, 2009, 6:28am
 
and which is the fastest way to do it? this is for an animation so i need to to that 30 in a seconds., i need the fatest way to do this.

Thanks
Re: arrays from objects to array
Reply #3 - Jul 30th, 2009, 7:38am
 
The fastest way, if the lengths of the arrays are all fixed, is just to loop:
Code:
for (int i = 0; i < 4; i++)
{
target[i] = o1[i];
target[i + 4] = o2[i];
target[i + 8] = o3[i];
}
Re: arrays from objects to array
Reply #4 - Jul 30th, 2009, 2:38pm
 
hi philho, i dont understand very well how your algorithm works. Can you explain please? i was trying to make something similar but i got no luck. My array doesnt copy successfully.  Why do you iterate 4 times?As far i understand the way you are doing works fine if you have little objects, but how about if i have a lot of objects?


  for (int i = 0; i < array_length * 4; i++)
{
 coords_target[i ] = objects[i/4].coords[0];
  coords_target[i + 1 ] = objects[i/4].coords[1];
 
}


objects is the array that contains objects , each object has an arraycoords, and im copying the content to coords_target, why does this doenst work?


thanks
Re: arrays from objects to array
Reply #5 - Jul 30th, 2009, 2:56pm
 
i<4 means the loop will run 4 times where i = 0,1,2,3
Remember that arrays start counting at zero so this equates perfectly to the four indices in your arrays.  'target[]' is a new array to which you want to copy the values from the other arrays.  So:

for (int i = 0; i < 4; i++) {
  // target [0],[1],[2],[3] will be populated with the values from object one array
  target[i] = o1[i];
 // target [4],[5],[6],[7] will be populated with values from object two array
  target[i + 4] = o2[i];
  // target [8],[9],[10],[11] will be populated with values from object three array
  target[i + 8] = o3[i];
}

That leaves you with the target array containing all the values from the three separate arrays in the order you want them Smiley
Re: arrays from objects to array
Reply #6 - Jul 30th, 2009, 3:55pm
 
blindfish explained properly my (untested) code, but I will answer the questions.
I based my answer on your first message, where apparently the arrays have all the same length, of 4 (hence the 4 iterations, you can replace it by array1.length), and that you have only three arrays.
We can provide code only fitting to your specifications...

If you have n objects, all with an array of same length, it can become:
Code:
int al = objects[0].coords.length;
for (int i = 0; i < al; i++)
{
for (int j = 0; j < objects.length; j++)
{
target[i + j * al] = objects[j].coords[i];
}
}

Still untested, it is late, I can have inverted some variables... Smiley
Re: arrays from objects to array
Reply #7 - Aug 2nd, 2009, 6:53am
 
hello phyllo, your example works fine when i have one dimensional array, but what about if my array that stores that objects  is 2 dimensional?

ive tried something like this without luck, any idea of what im doing wrong?

Code:



int al = objects[0][0].coords.length;
for (int i = 0 ; i < al ; i++)
{
for (int j = 0; j<objects.length ; j ++)
{
for (int k = 0 ; k < objects.length ; k ++)
{
target[i + j + k * al ] = objects[j][k].coords[i];
}}}



Page Index Toggle Pages: 1