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 › 2D Array: Remove Item
Page Index Toggle Pages: 1
2D Array: Remove Item (Read 875 times)
2D Array: Remove Item
Mar 23rd, 2010, 1:41pm
 
I tried searching for this, but ran into threads about sort. Now I don't necessarily think this is mutually exclusive of sorting, but at the moment I'm at a loss of how to approach this.


I have a Two-Dimensional Array:

Code:

 { 1, 2, 3, 4, 5, 6, 7 },
 { 0, 2, 3, 4, 5, 6, 7 },
 { 0, 1, 3, 4, 5, 6, 7 },
 { 0, 1, 2, 4, 5, 6, 7 },
 { 0, 1, 2, 3, 5, 6, 7 },
 { 0, 1, 2, 3, 4, 6, 7 },
 { 0, 1, 2, 3, 4, 5, 7 },
 { 0, 1, 2, 3, 4, 5, 6 }


I want to remove all 5's and the row that doesn't have any 5's. I was thinking that I would have to sort the array so that 5 was the last item in each array and then shorten the array. Then I would have to sort the arrays so that the 6th row was the last row and shorten that. Is this the best way to go about doing this?

Code:

 { 1, 2, 3, 4, 6, 7, 5 },
 { 0, 2, 3, 4, 6, 7, 5 },
 { 0, 1, 3, 4, 6, 7, 5 },
 { 0, 1, 2, 4, 6, 7, 5 },
 { 0, 1, 2, 3, 6, 7, 5 },
 { 0, 1, 2, 3, 4, 6, 7 },
 { 0, 1, 2, 3, 4, 7, 5 },
 { 0, 1, 2, 3, 4, 6, 5 }

// then

 { 1, 2, 3, 4, 6, 7, 5 },
 { 0, 2, 3, 4, 6, 7, 5 },
 { 0, 1, 3, 4, 6, 7, 5 },
 { 0, 1, 2, 4, 6, 7, 5 },
 { 0, 1, 2, 3, 6, 7, 5 },
 { 0, 1, 2, 3, 4, 7, 5 },
 { 0, 1, 2, 3, 4, 6, 5 },
 { 0, 1, 2, 3, 4, 6, 7 }

// then

 { 1, 2, 3, 4, 6, 7 },
 { 0, 2, 3, 4, 6, 7 },
 { 0, 1, 3, 4, 6, 7 },
 { 0, 1, 2, 4, 6, 7 },
 { 0, 1, 2, 3, 6, 7 },
 { 0, 1, 2, 3, 4, 7 },
 { 0, 1, 2, 3, 4, 6 }

Re: 2D Array: Remove Item
Reply #1 - Mar 23rd, 2010, 4:38pm
 
boolean hasFive=False;


for(int i=0;i<row.length;i++){

     for(int q=0;q<row[i].length;q++){
     if(row[i][q]==5){
           splice(row[i],q+1,row[i].length-1);
           hasFive=True;
           }      

   if(!hasFive){
      splice(row,i+1,row.length-1);
      }

}


Im not sure if  the code is written correctly but the concept is correct. Its going to take a row and walk over all its values. If it finds 5 in the row it will splice that value out.(I may have written the index incorectly)If the row doesn't have a 5 in it the hasFive flag will trigger and it will pop the whole row out of your matrix using the same splice method. If the row did have 5 it will continue merrily on its way. Then it walks over the next row.

I suggested doing it this way since you can easily make it into a function, it will remove multiple values of 5 and if u need you can just change the value you need to drop.
Re: 2D Array: Remove Item
Reply #2 - Mar 23rd, 2010, 5:25pm
 
Wow, this is really helpful and makes a lot of sense. Very concise, thanks very much!
Page Index Toggle Pages: 1