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 › change values in a range of arraylist elements
Page Index Toggle Pages: 1
change values in a range of arraylist elements (Read 643 times)
change values in a range of arraylist elements
Aug 14th, 2009, 9:29am
 
Hi,

I have written this bit of code to figure out whether elements in an arraylist equal and replace them with a different value if they do. I am struggling to figure out the code for finding and replacing numbers that are within a +2 or -2 range. Does anybody know how I can alter this statement

if ( (aList.get(i)).equals(aList.get(j)) == true)    

to include a range of values?

Thanks
       



import java.util.ArrayList;

void setup()
{
 ArrayList aList = new ArrayList ();

 aList.add(1.0);
 aList.add(4.0);
 aList.add(5.0);
 aList.add(5.0);
 aList.add(9.0);

 println("aList: " + aList);

 for (int i =0; i <= aList.size()-1;i++)
 {
   for (int j =0; j <= aList.size()-1;j++)
   {
     if(i!=j)
     {
       if ( (aList.get(i)).equals(aList.get(j)) == true)    
       {
         float smaller = (((Float) aList.get(i)).floatValue()) - 1;
         float larger = (((Float) aList.get(i+1)).floatValue()) + 1;
         aList.set(i, smaller);
         aList.set(i+1, larger);
       }
     }
     println("aList: " + aList);
   }
 }
}
Re: change values in a range of arraylist elements
Reply #1 - Aug 14th, 2009, 10:35am
 
How about (untested):

Edited:
actually doesn't look like this works (I'm not that familiar with ArrayList accessors), but the principal is still correct.  Will check and post some working code soon (hopefully)


Code:
if(abs(aList.get(i) - aList.get(j)) < 3) {
 //
}


abs() will convert a negative number to its positive equivalent and leave a positive number alone.  So for example if "aList.get(i) - aList.get(j)" returns -2, the absolute of that will be +2.  If the sum returns +2, the absolute value is still +2... which means you can then simply test whether the value is less than 3.
Re: change values in a range of arraylist elements
Reply #2 - Aug 14th, 2009, 11:14am
 
OK - this is a bit of a cop-out.  I'm not sure an ArrayList is appropriate in this case (it's intended to hold 'Objects' and I'm being a bit stupid at how you convert the contents to a number (it's been a long day!)) so I've changed your code to work with a standard array.  Now in future if you need to use objects instead of numbers you could use an ArrayList and reference the number value stored in the object that you wish to compare, but let's keep thing simple for the moment:

Code:
void setup() {
 // a standard array of floats
 float [] aList = {1.0,4.0,5.0,5.0,9.0};
 
 println(aList);
 
 for (int i =0; i <= aList.length-1;i++) {
   for (int j =0; j <= aList.length-1;j++) {
     if(i!=j) {
       // the effect of abs
       println((aList[i] - aList[j]) + "    abs= " + abs(aList[i] - aList[j]));
       // here's the condition implementing abs()
       if ( abs(aList[i] - aList[j]) < 3) {
       // note that to test equality using a standard number array is also simpler:
       // if (aList[i] == aList[j]) {  
         float smaller = aList[i] - 1;
         float larger = aList[i+1] + 1;
         aList[i] = smaller;
         aList[i+1] = larger;
       }
     }
   }
 }
 println(aList);
}


One thing you might want to look out for here is that you're comparing each pair of numbers twice - e.g. when i=1, j=2 is the same comparison as i=2,j=1 - so the changes you make to the values could get applied twice.  If you don't want this to happen do a search on optimising collision detection - there's a standard way to resolve this issue, and collision detection is where it's used most often Wink
Re: change values in a range of arraylist elements
Reply #3 - Aug 15th, 2009, 5:00am
 
I had this code using an array before. Is it right, that mine is not taking care of negative numbers but the way you did it is?
I also had a look at the collision detection but all I could find, is that they loop through the array backwards, which i tried but then the my code just doesn't do anything at all. Thanks for your help.

  float[] xPosCount = { 100.5 , 30.5 , 65.5 , 20.5 ,130.5, 10.0 };

         for (int m = 0 ; m < xPosCount.length; m++)          
         {                                          
          for (int n = 0; n < xPosCount.length; n++)
               {
               if(m!=n)
               {
                   if ((xPosCount[n])  >=  (xPosCount[ m]-30) )
                   {
                      float smaller = (xPosCount[n] - 15);
                     float larger = (xPosCount[m] + 15);
                     
                     xPosCount[m] =larger;
                     xPosCount[n] = smaller;
                     
                   }
                   
                    else if ((xPosCount[n]) <= (xPosCount[ m]+30))  
                   {
                     float smaller = (xPosCount[m] - 15);
                     float larger = (xPosCount[n] + 15);
                   
                    xPosCount[n] =smaller;
                    xPosCount[m]=  larger;
                   
                 }
               }
            }                                
         }
                      println(xPosCount);
Page Index Toggle Pages: 1