title: Using an ArrayList in Another Class to scan through objects
body: <p>I'm trying to scan though an AraayList created in the main class, in the turtle class to check various criteria. I simplified the code as much as possible for you. </p>
  
  <p>The main class: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import javax.swing.*;
  import java.awt.*;
  import java.util.ArrayList;
  import java.awt.event.*;
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
  
  class TurtleProgram
  { 
   public ArrayList&lt;DynamicTurtle&gt; turtles;
  
   public static void main(String[] args)
   {
    new TurtleProgram();
   }
  
   public TurtleProgram()
   {
    //INSTANTIATED JFRAME WITH BUTTONS/SLIDER AND CREATED LISTENER METHODS
  
    turtles = new ArrayList&lt;DynamicTurtle&gt;(); //THE ARRAYLIST I AM WANTING TO USE IN ANOTHER CLASS
    turtles.add(new RandomTurtleB(canvas, 400, 300, 100, 0)); //THIS ARRAYLIST BECOMES LARGER WHEN BUTTONS ON JFRAME TO ADD NEW TURTLES
    
    gameLoop(); //LOOP FOREVER
   }   
  
   private void gameLoop() 
   {
    int deltaTime = 20;
    
    while(true)
    {
     for (int i = 0; i &lt; turtles.size(); i++)
     {
      (turtles.get(i)).unDrawTurtle(); //REMOVE FROM CANVAS    
      (turtles.get(i)).wrapPosition((turtles.get(i)).getPositionX(), (turtles.get(i)).getPositionY()); //MAKE SURE TURTLES NOT OFF SCREEN
     }
  
     for (int i = 0; i &lt; turtles.size(); i++)
     {
      (turtles.get(i)).update(1000); //MAKE THE TURTLES MOVE WITH A CHANCE OF TURNING
     }
  
     for (int i = 0; i &lt; turtles.size(); i++)
     {
      (turtles.get(i)).drawTurtle(); //DRAW TO CANVAS   
     }
  
     for(int i = 0; i &lt; turtles.size(); i++)
     {
      (turtles.get(i)).cohesian(); //THIS IS HOW I WOULD LIKE TO ADDRESS THE COHESIAN METHOD
     }
    
    
    Utils.pause(deltaTime/2);
    } 
   }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Then I would like to check the current turtle against other turtles to see if it's close in the cohesion() method to perform actions: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class Turtle 
  {
      protected Canvas canvas; // private field reference to a canvas private           
      private CartesianCoordinate myLocation, oldLocation; 
      private boolean penDown = true;
      private double angle, maxX, maxY, nowPosX, nowPosY, maximumX, maximumY, x, y;
      public double d, e, first, second; 
      private int speed;   
  
      public Turtle(Canvas canvas, CartesianCoordinate initLocation) 
      {
          this.canvas = canvas;
          this.myLocation = new CartesianCoordinate(0,0);        
          penDown = true;
          myLocation = initLocation.copy();        
      }      
  
      public void cohesian()
      {
          double flockingDistanceLimit = 200;
          double numberOfFlockers = 0;
          double combinedX = 0;
          double combinedY = 0;
          double averageCombinedX, averageCombinedY, averageCombinedY,  moveToFlock, turnToFlock, distanceToPotentialFlock;        
  
          for (DynamicTurtle t : turtles) //CANNOT USE THE ARRAYLIST TO SCAN THROUGH ITS ELEMENTS
          {
              if(this.getPositionX() != t.getPositionX() &amp;&amp; this.getPositionY() != t.getPositionY()) //MAKE SURE TURTLE ISNT SCANNING ITS SELF
              {
                  distanceToPotentialFlock = Math.sqrt(Math.pow((this.getPositionX()-t.getPositionX()),2 ) + Math.pow((this.getPositionY()-this.getPositionY()),2)); //FIND DISTANCE BETWEEN CURRENT AND SCANNED TURTLE
  
                  if(distanceToPotentialFlock &lt; flockingDistanceLimit) //MAKE SURE THE FOUND TURTLE IS WITHIN RANGE USING THE DISTANCE BETWEEN POINTS METHOD
                  {
                      combinedX = combinedX + t.getPositionX(); //FIND SUMMATION OF X POSITIONS
                      combinedY = combinedY + t.getPositionY(); //FIND SUMMATION OF Y POSITIONS
                      numberOfFlockers++; //AS A FLOCKER HAS BEEN FOUND INCREMENT THE NUMBER OF FLOCKERS
                  }
              }
  
              if(numberOfFlockers &gt; 0)
              {
                  averageCombinedX = (combinedX / numberOfFlockers); //FIND AVERAGE X POSITION
                  averageCombinedY = (combinedY / numberOfFlockers); //FIND AVERAGE Y POSITION
                  moveToFlock = Math.sqrt(Math.pow(averageCombinedX,2 ) + Math.pow(averageCombinedY, 2)); //CALCULATE DISTANCE TO CENTER OF FLOCKING
                  turnToFlock = Math.atan(averageCombinedY / averageCombinedX);
  
                  if(turnToFlock &lt; 0)
                  {
                      turnToFlock = 360 - (-turnToFlock); //ADJUSTING FOR NEGATIVES
                  }
              }
          }
      }  
  
      public void wrapPosition(double x, double y) 
      {
          this.maxX = x;
          this.maxY = y; 
  
          if(maxX &lt; 0)
          {
              this.setPositionX(800);
          }
  
          else if(maxX &gt; 800)
          {
              this.setPositionX(0);
          }
  
          if(maxY &lt; 0)
          {
              this.setPositionY(600);
          }
  
          else if(maxY &gt; 600)
          {
              this.setPositionY(0);
          }        
      }
  }
  
      </code></pre>
  </div>
  </div>
  </p>
  
  <p>If I could have any help with using the ArrayList in the Turtle Class, in the cohesian() method, it would be greatly appreciated. Thank you in advance. </p>