Results 1 to 4 of 4
  1. #1
    colerelm is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Comparing two objects

    I have two objects, player1 and player 2. These two objects have properties: String first name,String last name, double weight and int grade. I want to create a method that takes in a player object and compares it with the players in my list. So, for example, something like this:

    Java Code:
     protected void find(T target)
      // Searches list for an occurence of an element e such that
      // e.equals(target). If successful, sets instance variables
      // found to true and location to the array index of e. If
      // not successful, sets found to false.
      {
        location = 0;
        found = false;
    
        while (location < numElements) 
        {
          if ([B]list[location].equals(target)[/B])
          {  
            found = true;
            return;
          }
          else
            location++;
        }
      }
    But when I pass in a player with the EXACT same attributes as a player in the list, the equals method still returns false. What do I have to do to compare two players so that
    Java Code:
    player1 = (George, Lemons, 180, 9)
    player2 = (George, Lemons, 180, 9)
    player1.equals(player2)
    returns true?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Comparing two objects

    You have to override the equals() method so that it does what you want. Because of the contracts for equals() and hashCode() you will probably have to implement both. There is a (rather old) JavaWorld article that discusses what's involved.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Comparing two objects

    Cross posted at Comparing two objects

  4. #4
    LucasH is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default Re: Comparing two objects

    Agreed, you definitely have to override the equals() method to make the object "understand" when it's equal to other Object

Similar Threads

  1. Comparing objects: lists and maps
    By LucasH in forum Advanced Java
    Replies: 7
    Last Post: 05-24-2011, 06:41 PM
  2. Issues with comparing two generic objects
    By FOUAD-SPIDER in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 04:39 AM
  3. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 01:40 AM
  4. Homework help involving comparing Color objects to Strings
    By SergeantJoKer in forum New To Java
    Replies: 12
    Last Post: 09-25-2010, 06:13 AM
  5. Replies: 1
    Last Post: 01-22-2009, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •