This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Use of of a Comparable object as a parameter to indexof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Use of of a Comparable object as a parameter to indexof" Watch "Use of of a Comparable object as a parameter to indexof" New topic
Author

Use of of a Comparable object as a parameter to indexof

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I tried the following simple test.



The output of the above test is



I understand why the second line is producing 3 but i dont understand why the first line is not finding the new an object with title="Croydon".

The API describes the indexof method as one that


Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.


I think the API is saying that if the object is null then it will return the index of the first occurrence of a null object from the list. If it is not null it will return the index of the first occurrence where the passed in object equals method returns trued. Shouldnt the the object created as cities3.indexOf(new Cities("Croydon") equal to the object added previously as cities3.add(new Cities("Croydon")); ?

Thanks

[Edit - changed code tags to quote tags to fix the page width - MB]
sachin potu
Greenhorn

Joined: Oct 22, 2011
Posts: 21
try overriding equals and hash code, may be this can help you
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

is CompareTo not used for comparison?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

compareTo() is used for ordering, but equals() is (usually) used to test for equality, and so is what indexOf will be using.

...in fact, the bit from the API documentation that you quote above specifically says that.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


Output:
IndexOf(new Croydon)- 5
IndexOf(city)-3
cities3.contains(new Cities("Croydon"))-true
new Cities("Croydon").equals(new Cities("Croydon"))-true

With overriding equals() ,it will work as "==" and It will see both objects as different.
cities3.contains(new Cities("Croydon"))-false
new Cities("Croydon").equals(new Cities("Croydon"))-false
Hope this helps you.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Thanks guys. I originally thought that compareTo could be used for checking for equality.
Thanks for clarifying this.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of of a Comparable object as a parameter to indexof
 
Similar Threads
ArrayList.indexOf
Cannot Collections.binarySearch() a collection that implements Comparable.
ArrayList indexOf
Generics and Comparable