Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Problem with contains() method in ArrayList

Thread view: 
Chanchal - 04 Jan 2006 06:04 GMT
hi,
          In an application i'm developing, i have a class say X and
an arraylist of X ie,

list = new ArrayList<X>() ;

and i have added an object of X into list

list.add(x); // x ib object of X

and i'm calling the contains() function of the arraylist to check if an
object of X, say 'y' is in there.

I have overridden the equals() method in X and it returns true for
x.equals(y).

But the contains() method returns false. what could be the reason.
please advice

Chanchal
Torkel Franzen - 04 Jan 2006 06:11 GMT
> I have overridden the equals() method in X and it returns true for
> x.equals(y).
>
> But the contains() method returns false. what could be the reason.

 That you haven't in fact overridden equals.

 
Chanchal - 04 Jan 2006 06:16 GMT
Hi Franzen,
   i haven't fully understood your comment that i haven't overridden
equals

   this is my equals() function

   public boolean equals(SaleItem saleItem){
       return (saleItem instanceof Service &&
this.name.equals(saleItem.getName()));
   }

   am i missing anything here??

Chanchal
Jean-Francois Briere - 04 Jan 2006 06:20 GMT
> this is my equals() function
>
>    public boolean equals(SaleItem saleItem){
>        return (saleItem instanceof Service &&
>this.name.equals(saleItem.getName()));
>    }

Here you are overloading the equals method (same name, different
parameter number / type).
You are not overriding it (same name, same parameter number / type).

Regards
Adam Maass - 06 Jan 2006 04:59 GMT
>> this is my equals() function
>>
[quoted text clipped - 6 lines]
> parameter number / type).
> You are not overriding it (same name, same parameter number / type).

IE, you need this (or something similar) instead (note that the type of the
parameter is Object, not SaleItem):

public boolean equals(Object other)
{
   if(other instanceof SaleItem)
   {
       SaleItem saleItem = (SaleItem)other;
       return this.name.equals(saleItem.name);
   }
   else
   {
       return false;
   }
}
Jean-Francois Briere - 04 Jan 2006 06:16 GMT
The equals method MUST have the following signature:

public boolean equals(Object obj)

Regards
Chanchal - 04 Jan 2006 06:23 GMT
oooooooppppsssssssss, silly me,

thankx pal

Chanchal
Thomas Hawtin - 04 Jan 2006 12:30 GMT
> The equals method MUST have the following signature:
>
> public boolean equals(Object obj)

Adding @Override to methods that are supposed to override will cause the
compiler to catch these sorts of errors.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chanchal - 05 Jan 2006 07:00 GMT
hi thomas

  thanks for that tip. but where should i add @Override? should the
signature of function be

@override public boolean equals(Object obj) ? where can i find similar
swithces??

Chanchal
Chanchal - 05 Jan 2006 07:00 GMT
hi thomas

  thanks for that tip. but where should i add @Override? should the
signature of function be

@override public boolean equals(Object obj) ? where can i find similar
switches??

Chanchal
Jean-Francois Briere - 05 Jan 2006 09:07 GMT
The @Override annotation is not a part of the method signature.
It is just information to the compiler.
It's more like:

   @Override
   public boolean equals(Object obj)
   {
       ...
   }

Take a look at:
http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html

Regards
Thomas Hawtin - 05 Jan 2006 13:04 GMT
> The @Override annotation is not a part of the method signature.
> It is just information to the compiler.
[quoted text clipped - 5 lines]
>         ...
>     }

For @Override only, some people will write that as:

    @Override public boolean equals(Object obj)

Not sure how widespread that is. We still haven't got everyone to put
opening braces at end of line.

> http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html

And the JavaDocs for @Override itself. Annotations are hidden right at
the bottom of the type lists for packages (far bottom left). Note, Java
is case sensitive, so always a capital O.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.