• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting an exception with compareTo  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to Player
at Player.compareTo(Player.java:1)

This kind of relates to my Generic question I asked early but not fully,I am getting a cast exception and I don't know why I have looked over my code countless of times and I can't see the bug,am I overlooking something simple?

thanks

Main.java



Player.java

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also when I try to call Collections.binarySearch(p2, "Tim"); with two parameters it does not compile even though I have met the method guidelines

binarySearch(List<? extends Comparable<? super T>> list, T key)

I passed a list in that implements Comparable and a key in which is "Tim"
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and finally in the second BinarySearch method

binarySearch(List<? extends T> list, T key, Comparator<? super T> c)

it does not say that the type has to implement Comparable

rather it says List<? extends T> what does it mean by this?

and why when I don't implement comparable I don't get a compile error but a runtime exception when I remove the implements Comparable and compareTo() method?

thanks

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry to post again so many posts I found the problem with my first question I should have passed the object Tim in not the String Tim into the BinarySearch

but my other two questions I'm still confused on


 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would search for a String "Tim" in a collection of FootballPlayers.

Do you see a problem?

You were not able to call Collections.binarySearch(p2, "Tim") because the types didn't match.
In binarySearch(List<? extends Comparable<? super T>> list, T key), T can not apply both to String and FootballPlayer.


In binarySearch(List<? extends T> list, T key, Comparator<? super T> c) you are allowed to pass alist of objects that don't implement Comarable because they wouldn't be compared using Comparable.compareTo().
They would be compared using c.compare().

And please don't pass null Comparators.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last week I told somebody else that Comparable implies a total ordering, and I showed you how you might order people with initials AZ and ZA and how people might sort AZ before ZA or ZA before AZ or by age. Here is a link; have a look at the links in that post, too. If you make Player implement Comparable, that implies three things:-
  • 1: There is a total ordering: each Player is more than or less than or the same as every other Player.
  • 2: The ordering is always done the same way, using the same criteria.
  • 3: You shou‍ld only be comparing Player objects to other Player objects. You have of course realised that you can't compare Strings because Strings have a different sort of comparison.
  • The way to make sure you only compare to Player objects is to use Comparable's generics:-
    public class Player implements Comparable nonononononononononononono
    public class Player implements Comparable<Player> yesyesyesyesyesyesyesyesyesyes
    That means you can write the compareTo method differently:-That was how we had to write it until Java1.4.2.
    Now Java5 is here (been and gone), you can write this:-No cast, no risk of such exceptions at runtime. Note you shou‍ld still throw an Exception if null is passed to compareTo.

    Do you really want Player to implement Comparable? Do players have a natural ordering? What if you order them by name, number of goals, wages (use BigDecimal not int or double, to avoid risk of overflow to ∞), league played in? Each will give you a different ordering, so you can create different Comparator objects to sort by name, goals, wages (BigDecimal already implements Comparable, so that makes it easier) etc. Go back to the first link in this post, and open that link, the follow its first link to the Java™ Tutorials←copy here. I think that is a very useful resource.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!