[Bluej-discuss] equals method with an ArrayList

Michael Kölling M.Kolling at kent.ac.uk
Fri Feb 3 10:05:14 GMT 2006


It also depends on which 'add' method you are talking about.

The ArrayList allows duplicates, so ArrayList::add does not check for  
equality at all. Only Sets chack for equal elements.

Regards,

Michael

On 2 Feb 2006, at 22:32, Louise Laforest wrote:

> You have to override the method equals from Object class.  Here, you
> just overload it, because when called, if the parameter is not from
> class Player or other derived class from it, the method equals from
> the class Object is called.  So, your method should look like this in
> your Player class :
>
> public boolean equals(Object anotherPlayer) { ...
>
>
> Louise
>
> Le 06-02-02 à 16:44, Yves Hamel a écrit :
>
>> Hi,
>>
>> I have a problem with an ArrayList that I just can't solve.
>>
>> Here's the code :
>>
>> public class Player {
>>
>>     private String name;
>>
>>     public Player(String name) {
>>
>>         this.name = name;
>>     }
>>
>>     public boolean equals(Player anotherPlayer) {
>>
>>         return this.name.equals(anotherPlayer.name);
>>     }
>> } // Player
>>
>>
>> public class MultiSet<T> {
>>
>>
>>     private ArrayList<T> elements = new ArrayList<T>();
>>
>>
>>     public boolean isElement ( T element ) {
>>
>>         boolean elementFound = false;
>>
>>         for (int i = 0; i < this.elements.size() && !elementFound; i
>> ++) {
>>             if (elements.get(i).equals(element)) {
>>                 elementFound = true;
>>             }
>>         }
>>
>>         return elementFound;
>>     }
>> }
>>
>> There is another method that adds the elements to the ArrayList.
>>
>> So I create two players withe same name and then I add the first
>> one to the ArrayList. Before adding the second one, I expect the
>> add method to check if there is already another object equals to an
>> existing object in the ArrayList. It works if I use String objects.
>> but as soon as I use my objects, it doesn't work. It doesn't even
>> go into the equals method of the Player class. If I print the class
>> names from the isElement() method it gives me Player for both
>> elements. If I use the equals() method to compare two players
>> directly, it works. My opinion is it looks for the equals method of
>> the Object class but I don't understand why. Can you help me?
>>
>> Thanks!
>>
>> Yves
>>
>> _______________________________________________
>> mailing list bluej-discuss at bluej.org
>> To unsubscribe or change your preferences, go to
>> http://lists.bluej.org/mailman/listinfo/bluej-discuss
>>
>
> -----------------------------------------
> Louise Laforest, Ph.D.
> Professeure
> Université du Québec à Montréal
> Département d'informatique
> téléphone (514) 987-3000 poste 7790
> fax (514) 987-8477
> laforest.louise at uqam.ca
>
>
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss



More information about the bluej-discuss mailing list