title: ArrayList indexOf() returns wrong index?
body: <p>I have a problem with ArrayList. I'm using ArrayList like this:</p>
  
  <pre><code>private ArrayList&lt;Playlist&gt; mPlaylists;
  </code></pre>
  
  <p>where Playlist is a class inherited from another ArrayList.
  I do the following:</p>
  
  <pre><code>p = new Playlist(...some parameters...);
  mPlaylists.add(p);
  </code></pre>
  
  <p>Later, when I use 'p' to get the index in the list:</p>
  
  <pre><code>int index = mPlaylists.indexOf(p);
  </code></pre>
  
  <p>an index of '1' is returned, even though inspection of the list clearly shows that it's index '4'.</p>
  
  <p>Does anybody know why this fails?
  Thanks.</p>
  
  <p>B.R.
  Morten</p>
  
  <p><strong>Edit:</strong>
  Same problem without indexOf(), using equals():</p>
  
  <pre><code>private int GetIndex(Playlist playlist) {
      for (int i = 0; i &lt; mPlaylists.size(); i++) {
          if (mPlaylists.get(i).equals(playlist)) {
              return i;
          }
      }
      return -1;
  }
  </code></pre>
  
  <p><strong>New edit:</strong>
  This WORKS!:</p>
  
  <pre><code>private int getIndex(Playlist playlist) {
      for (int i = 0; i &lt; mPlaylists.size(); i++) {
          if (mPlaylists.get(i) == playlist) {
              return i;
          }
      }
      return -1;
  }
  </code></pre>
  
  <p><strong>Solution:</strong>
  As suggested, I changed the Playlist class to not enherit from ArrayList, but rather keeping an instance privately. It turned out that I only had to implement 4 ArrayList methods.</p>
  
  <p>This does the trick; Now indexOf() returns the correct object!</p>
  
  <p>Thanks to all contributors!</p>