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 › arrange two dimension array
Page Index Toggle Pages: 1
arrange two dimension array (Read 292 times)
arrange two dimension array
May 4th, 2008, 12:18am
 
Hello,

I am a beginner with processing, and due to limited knowledge I'm not able to rearrange simple list of array using array functions available in processing. It would be a great help if you could show me the way. Here is the problem

I have two dimension array like this

float notes[][] = {{60,1.0},
                  {64,1.0},
                  {62,1.0},
                  {65,1.0},
                  {67,1.0},
                  {71,3.0},
                  {69,1.0},
                  {72,4.0}};

I want to rearrange the order of notes array according to a sequence from another variable hit[i] which could be (4, 7, 1, 0, 3)

The result should be
newNotes = {{67,1.0},
           {72,4.0},
           {64,1.0},
           {60,1.0},
           {65,1.0}}

I tried with arraycopy and subset to get each elements for example hit[7] = {72, 4.0} but how can I put them together.
concat doesn't work, Is there any other way?

Thanks a lot!!!

Re: arrange two dimension array
Reply #1 - May 4th, 2008, 1:35am
 
I also am a beginner at this. The following works, though it does require declaring the second array with the size of the change array:

int[] change = {4,7,1,0,3};
float[][] notes = {
{60,1.0},  
   {64,1.0},  
   {62,1.0},  
   {65,1.0},  
   {67,1.0},  
   {71,3.0},  
   {69,1.0},  
   {72,4.0}};
float[][] newNotes = new float[change.length][2];
int i;
for (i=0;i<change.length;i++) {
 newNotes[i]=  notes[change[i]];
}

println("newNotes.length is "+newNotes.length);
for (i=0;i<change.length;i++){
println(" ");
print("newNotes["+i+"][0] "+newNotes[i][0]);
print(" ");
print("newNotes["+i+"][1] "+newNotes[i][1]);
}

An alternative is to make the second array big enough and just use part of it.

Some on-line posts say that dynamic arrays are bad...

Page Index Toggle Pages: 1