• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Cannot Collections.binarySearch() a collection that implements Comparable.

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im struggling to understand why these two statements are generating a compiler error



The code used is shown below


And the instances are initialised as follows



The error being generated is shown below


- Error 1 is complaining about the cities object. The Cities class implements Comparable so why is it not allowing it
- The second error is complaining about cities2. This is based on the CitiesNoComparable class. This class does not implement comparable so this error is probably justified.
- The third error is based on the same object but i am passing a Comparator in the search. Shouldnt this have been accepted?

Thanks
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
- Error 1 is complaining about the cities object. The Cities class implements Comparable so why is it not allowing it
- The second error is complaining about cities2. This is based on the CitiesNoComparable class. This class does not implement comparable so this error is probably justified.
- The third error is based on the same object but i am passing a Comparator in the search. Shouldnt this have been accepted?



It's not complaining about the Cities or CitiesNoComparable class. It's complaining about key parameter to the binarySearch method. This parameter is the same type as the generic (or super); and in both cases, you provided a string, ie. "London". It needs to be either a Cities or CitiesNoComparable instance.

Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yes ofcourse. I was supposed to search for an object with "London" as its city. Not the actual city itself as a string.



Thanks
 
straws are for suckers. tiny ads are for attractive people.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic