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.
IndexDiscussionExhibition › Identifying the greatest ArayList number problem.
Page Index Toggle Pages: 1
Identifying the greatest ArayList number problem. (Read 1158 times)
Identifying the greatest ArayList number problem.
Nov 2nd, 2009, 5:22pm
 
I am trying to writing something that identifies which Array number is the greatest present under a certain situation. I have this if statement that tells me when an ArrayList object is inside the radius of another object, and I'm trying to writing something so that that object can identify which of the present objects is greatest in the ArrayList. Does that make sense?

--------------------------------------------------------------------------------


for(int i=enemies.size()-1; i>=0; i--){
     Enemy peng = (Enemy) enemies.get(i);

     float dis = dist(x,y, peng.x,peng.y);
     float addedRadii = 8+diameter/2;
     TowMiliSec++;

     if(dis <= addedRadii){

       
       /*
          I tried to write something here that can tell me
          which ArrayList Object has the greatest 'i' under
          the circumstances of this if statement. For example,
          lets say the objects with the 'i' 8, 7, 6, 5 are inside this
          radius (or "if statement"). How could I write something that
          will spit out the number 8, seeing that it's the largest
          number out of those four?
       */

       
       float biggestFac = max(x-peng.x,y-peng.y);
       float fac = biggestFac/3;

       float vx = x/fac;
       float vy = y/fac;

       if(peng.x<x){
         vx *= -1;
       }
       if(peng.y<y){
         vy *= -1;
       }

       if(TowMiliSec%60 == 0){
         if(control3 == 0){
           bullets.add(new Bullet(x,y, vx,vy, false, damage));
           control3++;
         }
       }
       else{
         control3 = 0;
       }
     }

     for(int j=bullets.size()-1; j>=0; j--){
       Bullet bull = (Bullet) bullets.get(j);
       bull.move();
       bull.impact();      

       if (bull.hit()) {
         bullets.remove(j);
       }
     }
   }
Re: Identifying the greatest AraayList number problem.
Reply #1 - Nov 2nd, 2009, 7:30pm
 
I do that by looping through each number, setting a "maxNum" variable.

int maxNum=0;
for (int i=0; i<nums.length; i++){
 if (nums[i] > maxNum) maxNum = nums[i];
}

I know this section says "and work in progress," but this sort of thing usually goes in Programs or Syntax Questions, for future reference...
Re: Identifying the greatest ArayList number problem.
Reply #2 - Nov 2nd, 2009, 8:24pm
 
Oh, sorry, I'm still new to processing and the forum. Ummm, I know this might sound like a stupid question for you, but how do define nums? How do you tell it how many i's are present under that if statement?
Re: Identifying the greatest ArayList number problem.
Reply #3 - Nov 2nd, 2009, 9:28pm
 
well, in that example I meant for "nums" to represent an array of ints (integers) ... and the loop syntax means 'start with a temporary int var called i, and increment it by one until it reaches the length of the "nums" array'...I wrote a brief tutorial on this stuff, which may help:
http://benhem.com/dev/tutorial.htm
Re: Identifying the greatest ArayList number problem.
Reply #4 - Nov 2nd, 2009, 9:40pm
 
so, i have to make a new array to keep track of the i's in the array list?
Re: Identifying the greatest ArayList number problem.
Reply #5 - Nov 3rd, 2009, 12:15am
 
no, sorry, that was unclear.  That example was just to show the general form: how to identify the maximum number from a set of numbers...you can create and access that data any way you like, maybe variables owned by the enemy class in your ArrayList / distances calculated in a separate loop, whatever you need to do.
Re: Identifying the greatest ArayList number problem.
Reply #6 - Nov 3rd, 2009, 6:54am
 
Potentially I'm not understanding the problem, but, since you're counting the i's backwards from enemies.size() to 0, the greatest i is always going to be the first one that you hit, right?  So you could just do

maxNumI = -1

outside of the loop

and then,

if (dis <= addedRadii && maxNumI == -1){
//do something with i, here, to keep track of it, or whatever
}
Page Index Toggle Pages: 1