• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

facing exception in implementation of binarySearch method.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic