• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Doesn't This Compile?

 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code doesn't compile. It is from a question at CertPal.com



Looking at the API, I thought that it would have compiled since sort() is defined as follows: ]link

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API for the sort method you are using states:

All elements in the array
must implement the Comparable interface.
 
Chris Johansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I believe the compilation fails because it is unable to determine the order of the elements. It has no rules on how objects of type "Compare" are supposed to be ordered. So for this to compile, you'd need to implement "Comparable" and its mandatory method "compareTo". Happy reading!
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles for me. (It throws an exception if you try to run it.) Either the question is wrong, or you misunderstood it.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you done?
John
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John, that was it. The following does compile but throws a ClassCastException when it is run. I feel that I understand this code now.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic