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?
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.