title: loop to get closest object to input value
body: <p>I have this Martian class: </p>
  
  <pre><code>public abstract class Martian implements Cloneable {
  int id;
  
  public Martian(int id) {
      this.id = id;
  }
  public Object clone() throws CloneNotSupportedException {
      return super.clone();
  }
  public int getId() {
      return id;
  }
  public boolean equals(Object o){
      if( o != null);
      return this.getId() == ((Martian)o).getId();    
  }
  public abstract void speak();
  
  public String toString(){
      return "Martian" + getId(); 
  }       
  } 
  </code></pre>
  
  <p>and a MartianManager class: </p>
  
  <pre><code>public class MartianManager {
  private ArrayList&lt;Martian&gt; martians;
  private ArrayList&lt;Martian&gt; teleporters;
  
  public void addMartian(Martian m) {
      martians.add(m);
      if(m instanceof GreenMartian)
          teleporters.add(m);
  }
  //public Object clone() {
  
  public Martian getMartianClosestToID(int id) {
  
  }
  public void groupSpeak() {
      for(Martian m : martians) {
          m.speak();
      }
  }
  public void groupTeleport(String dest) {
      for (Martian m : martians){
          if (m instanceof GreenMartian)
              ((GreenMartian) m).teleport(dest);
      }   
  }
  //public obliterateTeleporters() 
  
  //removeMartian(int id)
  }
  </code></pre>
  
  <p>In the <code>MartianManager</code> class I have a method <code>getMartianClosestToId()</code> which returns the martian with the id closest to the input id. My question basically is what would be the simplest logic to use in a loop to do this, or is their an easier way to do it such as <code>compareTo</code> which I don't know if compare would even work in a situation like this.</p>