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

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: 23951
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
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic