Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

facing exception in implementation of binarySearch method.

 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
 
K. Tsang
Bartender
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic