wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java.lang.Comparable and classcastexception probs yo - from OCP Exam 2 by K and B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java.lang.Comparable and classcastexception probs yo - from OCP Exam 2 by K and B" Watch "java.lang.Comparable and classcastexception probs yo - from OCP Exam 2 by K and B" New topic
Author

java.lang.Comparable and classcastexception probs yo - from OCP Exam 2 by K and B

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

Hello Java_Ranch (echo echo echo),

I am having big-time-major-difficulties solving a java programming problem. I have hacked up the code from a question on Exam 2 of the OCP book of practice exams by K and B to come up with this monstrosity. The problem is, I receive a compiler error : Exception in thread "main" java.lang.ClassCastException: VLA cannot be cast to j
ava.lang.Comparable
If someone could tell me what I am misunderstanding I would appreciate.



***NOTE: if i use the comparator class at the bottom everything is blue skies and green lights - the program does not halt because of a runtime exception and the array is sorted.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

After posting... I saw this with new eyes. If I change the class to implement Comparable instead of Comparator everything is absolutely fine. The stars sing again...


sorry

nevermind
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1


Here is the code from question 40 from Exam 2 in the OCP Java SE 6 Programmer Practice Exams: (Exam 310-065) by Bert Bates and Katherine Sierra.




*** Still, somewhat confused here - This code does implement the Comparator interface and does have a valid override for it with the compare method. Why doesnt the array sort based on this?
The answer from the OCP book says, "Arrays.sort() assumes that the elements of the array to be sorted implement Comparable unless you provide a Comparator" [which is done].
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ted North wrote: [which is done]


No, it isn't. Your call to Arrays.sort() simply passes the array to be sorted. It doesn't pass a Comparator. Check the API documentation for the Arrays.sort methods to see which versions take a Comparator as a parameter, then change your code to use one of those versions.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
Searches a range of the specified array for the specified object using the binary search algorithm.

static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
Searches the specified array for the specified object using the binary search algorithm.

---

Thank you for the response. Above I copy-pasta'd the binarySearch methods from the java.util.Arrays API that pass a comparator as arguments. My thinking was that if the class implemented comparator successfully then the sort method would process this and sort according to the overridden comparator.

So, can I sort by passing the array-name and then a new instance of the class?

I just tried this and it doesn't work:

It seems like it would though because the class does implement the comparator interface and override the compare method.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
By the way, in this example, Arrays.sort(va) won't work because va does not contain comparable, like Integer, Float, String...
So, Arrays.sort method either takes an array of comparable or an array of elements and use a comparator to define how to sort (sort the array in ascending or descending order, sort the element's data in a customized order, eg in real application example, sort students based on their SSN or sort students based on their names....)

If you use this Arrays.sort(va, new VLA(678)), it works. It sorts the array in ascending order.
Look at the compare method (VLA a, VLA b) { return a.dishSize - b.dishSize;}

For simplicity, we have an array of 2 integer.
The sort method will pass array[0] as a and array[1] as b. if a-b > 0, then the sort method will swap it. If a-b<0, then the sort method won't swap it.


Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

Helen Ma wrote:By the way, in this example, Arrays.sort(va) won't work because va does not contain comparable, like Integer, Float, String...
So, Arrays.sort method either takes an array of comparable or an array of elements and use a comparator to define how to sort (sort the array in ascending or descending order, sort the element's data in a customized order, eg in real application example, sort students based on their SSN or sort students based on their names....)

If you use this Arrays.sort(va, new VLA(678)), it works. It sorts the array in ascending order.
Look at the compare method (VLA a, VLA b) { return a.dishSize - b.dishSize;}

For simplicity, we have an array of 2 integer.
The sort method will pass array[0] as a and array[1] as b. if a-b > 0, then the sort method will swap it. If a-b<0, then the sort method won't swap it.




You are correct. WOW. It does compile, run, and sort the objects... I am not sure why my program was not working yesterday. Thank you for chiming in and bringing this to my attention Helen. I made a new directory and compiled the code from there and ran it and it works!
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

Paul Clapham wrote:
Ted North wrote: [which is done]


No, it isn't. Your call to Arrays.sort() simply passes the array to be sorted. It doesn't pass a Comparator. Check the API documentation for the Arrays.sort methods to see which versions take a Comparator as a parameter, then change your code to use one of those versions.


****
static <T> void sort(T[] a, Comparator<? super T> c)
Sorts the specified array of objects according to the order induced by the specified comparator.
static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.

***
Oops I put the wrong method signatures in my above post. I pasted binarySearch() instead of Arrays.sort(). :/ Not sure why I am so spastic.

These methods are very similar to binarySearch though in that they take an array and a comparator as arguments...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.Comparable and classcastexception probs yo - from OCP Exam 2 by K and B
 
Similar Threads
Please explain the code.
Need help with practice question implememting Comparator Interface
SCJP Objective 6.5 Question
BinarySearch
Arrays java.util collections class - binarySearch() method