title: Java method: Finding object in array list given a known attribute value
body: <p>I have a couple of questions actually.</p>
  
  <p>I have a class <strong>Dog</strong> with the following instance fields:</p>
  
  <pre><code>private int id;
  private int id_mother;
  private int id_father;
  private String name="";
  private String owner="";
  private String bDate="";
  </code></pre>
  
  <p>I also have a class <strong>Archive</strong> which can instantiate <strong>Dog</strong> and put Dog objects into an ArrayList.</p>
  
  <p>I am trying to write a method in <strong>Archive</strong> which takes an integer as ID and looks through the ArrayList, and returns the object containing that ID.</p>
  
  <pre><code>private Dog getDog(int id){
      Dog dog = new Dog();
      int length=getSize();
      int i=0;
  
      dog=al.get(i);
      i++;
  
      while(dog.getId()!=id &amp;&amp; i&lt;length)
          dog=al.get(i);
          i++;
  
      if(dog.getId()!=id)
          dog=null;
      return dog;
  }//end getDog
  </code></pre>
  
  <p>There are two problems with this method (the other methods I use work). First of all it's not working, and I can't see why. I'm while-looping through (potentially) all the objects in the arraylist, for then after the loop is finished, checking whether the loop finished because it ran out of objects to search through, or because it found an object with the given ID. Secondly, that seems like an immensely time-consuming process. Is there some way to speed this up?</p>