title: Game design: organizing object arraylists
body: <p>To learn Java, I am making a classic "fly around in space" 2d game. Besides the player object, numerous enemies/obstacles (scavengers, hunters, comets, asteroids) exist, each with their own class that extends a GameObject class. As there can be several scavengers, comets etc these are stored in arraylists. However, as each object can interact with each other, there is a lot of looping and duplicate code to have e.g each alien interact according to the objects in the comet arraylist, the asteroid array list and so on.</p>
  
  <p>In my game update function I have:</p>
  
  <pre><code>public void update() {
  
      ArrayList&lt;Rock&gt; rockList = rock.getRockList();
      ArrayList&lt;Scavenger&gt; scavengerList = scavenger.getScavengerList();
      ArrayList&lt;Hunter&gt; hunterList = hunter.getHunterList();
      ....
      npc.update(player, scavengerList, hunterList, rockList);
      ...
  </code></pre>
  
  <p>}</p>
  
  <p>and in my NPC class (which extends the GameObject class)</p>
  
  <pre><code>public void update(Player player, ArrayList&lt;Scavenger&gt; scavengerList, ArrayList&lt;Hunter&gt; hunterList, ArrayList&lt;Rock&gt; rockList) {
  
      for(int i = 0; i &lt; scavengerList.size(); i++) {         
          scavengerList.get(i).update(player,scavengerList, ,hunterList rockList);
      }
      for(int i = 0; i &lt; hunterList.size(); i++) {            
          hunterList.get(i).update(player,scavengerList, hunterList, rockList);
      }
  ...
  }
  </code></pre>
  
  <p>And finally I have an update function in my scavenger class, my hunter class etc such as</p>
  
  <pre><code>public class Hunter extends NPC{
  ...
  public void update(Player player,ArrayList&lt;Scavenger&gt; scavengerList, ArrayList&lt;Hunter&gt; hunterList, ArrayList&lt;Rock&gt; rockList) {
  "update all hunter objects according to the player, scavengers, rocks etc"
  }
  </code></pre>
  
  <p>This approach seems to be rather cumbersome and as more classes are created the number or arraylists that needs to be parsed and looped through are getting out of hand. 
  Can anyone recommend a better way of doing this?
  I guess the obvious way would be to have one list containing all NPC objects and then keeping track of their class type and update accordingly.
  Is this a better way of doing it or can anyone point me in the right direction? </p>