It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Arraylist with Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arraylist with Objects" Watch "Arraylist with Objects" New topic
Author

Arraylist with Objects

Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Hey,
I have a bit of a complicated situation here. I am using the Arraylist class to store an array of objects of class Pair. The Pair class is a class I have defined in another .java file and works fine. It has a method called "equals()":

Now what I am trying to do is implement an "indexOf()" method in my main .java file to where it will search the arraylist and find the "Pair" that has the same name as the one I am searching for. Name is a Sring variable defined in the constructor of the Pair class. Maybe there isn't an easy way to do this, but if there is, I'd like to know how. Here is what I was trying:

table is my Arraylist. I found out from the API that the indexOf method of the arraylist calls the "equals()" method to figure out if it found the right object or not. So that's why I wrote my own equals method. But that equals method is not getting called (I found that out from testing). So how can I make the indexOf() method call my equals() method? Any help would be appreciated!


Want a free iPod, digital camera, or $300 cash? Completely legit offer at <a href="http://www.geocities.com/pricklymustard/freestuff.html" target="_blank" rel="nofollow">http://www.geocities.com/pricklymustard/freestuff.html</a> . I am looking for referrals which is why I need YOU!
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Originally posted by Justin Porter:
... I found out from the API that the indexOf method of the arraylist calls the "equals()" method to figure out if it found the right object or not. So that's why I wrote my own equals method. But that equals method is not getting called (I found that out from testing). So how can I make the indexOf() method call my equals() method? Any help would be appreciated!


Okay, so as you have noted that indexOf() method uses the equals() method...
Below is the signature of your equals() method...

as you can see, your method takes a String as an arguement...
But, this is where the issue comes in, the ArrayList indexOf() method uses the equals method whose takes a Object as an argument...

so basically you have overloaded the equals() method instead of overriding it...
your class now has two equals methods... the one inherited from Object and the overloaded one that you created...

to fix this, you need to override the inherited method, in essence change your method parameters to take an Object as an arguement instead of a String...

Hope this helps...


- Liam...<br />- ' He who never sleeps... '
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Ok thanks, I'll try it out. Definitely makes sense though, guess I should have checked parameters! :-)
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Still I cannot get it to work... here is my updated code:

And in my Pair class

Anything helps!
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Okay, this jumped out at me...



When comparing String values, you should use String's .equals() method, because it compares String values (characters) and not the String's (object) references...

you can also use String's equalsIgnoreCase() method, if you don't want the comparison to be case sensitive...
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Thanks for pointing that out. It would have to have been addressed later and I would have probably forgotten about having to compare strings with the equals method. It still doesn't fix my problem though. I have a println statement in my code that will let me know if my equals method has been run or not, and so far I've not been able to get that message printing yet. Thanks for the help so far, still looking for answers though!
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Here is an example using the equals() method...

as you can see, the simple overridden equal method only check to see if two DataHold objects have the same String value for data...

remember if two things are equal to each other, the the reverse must be true... In you equals method, you are taking attempting to compare a String with you Pair object, and the two are not equal because they are different types...
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You have declared your equals() method correctly, and yet it still isn't being called when you call indexOf() with a String. The indexOf() method compares the element you passed in with each of the elements in the list until it finds a match, but you're making it compare apples and oranges (Strings and Pairs).

You've set up your equals() method to handle comparing to a String, but if you check the source code of ArrayList (I highly recommend this when you get stuck with a Java native class) you'll see the problem:Here elem is the String you passed it. The problem is that String's equals() method is being called instead of yours. Your only choice if you want to use the built-in indexOf() method is to create a Pair using the name you're looking for before calling it. Otherwise you'll need to write your own indexOf() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arraylist with Objects