This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes facing exception in implementation of binarySearch method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "facing exception in implementation of binarySearch method." Watch "facing exception in implementation of binarySearch method." New topic
Author

facing exception in implementation of binarySearch method.

Nakul P. Patel
Greenhorn

Joined: May 31, 2011
Posts: 25


I want to perform search operation on List of objects.The sorting of ascending and descending is working fine facing exception in implementation of binarySearch()
I am preparing for OCPJP. Please let me know briefly(if possible) where i am making mistake.

Thanks in advance
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Did you consult the javadoc? There are 2 binarySearch() methods. One with 2 without comparator and one with 3 with comparator.

I think you can use the 2 parm method to get what you want.

And when you calling binary search, which list are you using? The natural order one or the reverse order one?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Nakul P. Patel
Greenhorn

Joined: May 31, 2011
Posts: 25
[quote=

I think you can use the 2 parm method to get what you want.



I am doing this for learning purpose.I intentionally want to use method with three param.
[quote=

And when you calling binary search, which list are you using? The natural order one or the reverse order one?

Before doing search on list, the list must be sorted.Does it matter which one list i am using to search?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Nakul P. Patel wrote:Before doing search on list, the list must be sorted.Does it matter which one list i am using to search?


I think so. Because the returned index will be different. Look for 2 in natural order list gives the index 1 assuming it's 0-based. Same search in reverse order list gives second to last index, if 10 items indexed 0 to 9, it will return 8.

If you plan to use the 3 parm method, then pass in the a "comparator" not a Student.
 
Consider Paul's rocket mass heater.
 
subject: facing exception in implementation of binarySearch method.
 
Similar Threads
Asking for help with Sort-Program (Arrays)
collections.binarySearch() is not working with comparable
Sorting
Regarding TypeSafe Iterator
Why The Output Is Same...