previous | start | next

## The `equals` Method

- `equals` tests for equal contents
- `==` tests for equal location
- Used in many standard library methods
- Example: `ArrayList.indexOf
  `

      /**

         Searches for the first occurrence of the given argument,

         testing for equality using the equals method.

         @param elem an object.

         @return the index of the first occurrence

         of the argument in this list; returns -1 if

         the object is not found.

      */

      public int indexOf(Object elem)

      {

         if (elem == null)

         {

            for (int i = 0; i < size; i++)

               if (elementData[i] == null) return i;

         }

         else

         {

            for (int i = 0; i < size; i++)

               if (elem.equals(elementData[i])) return i;

         }

         return -1;

      }

previous | start | next