Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with an array sort problem

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the code. What is the result?



A) "2 1" is printed.
B) "1 2" is printed.
C) Compilation fails.
D) An exception is thrown at runtime.


WHAT IS THE ANSWER, AND WHY?

My answer is D, because when you execute the line Arrays.sort(arr);, strings and HashTest objects are not comparable, so an exception would be thrown.

The given answer to this is D, but with this explanation: A ClassCastException is thrown: HashTest cannot be cast to java.lang.Comparable
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rachel,

If you check the documentation of Arrays.sort, it says that when an array of Object is passed to Arrays.sort, all the members of the array must implement Comparable interface.

Technically, it doesn't matter if actual objects are of different class as long as all those classes implement Comparable (or, to be specific, Comparable<Object>).

In your code, HashTest class does not implement Comparable interface, and hence the ClassCastException.

I hope this helps.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Hi Rachel,

If you check the documentation of Arrays.sort, it says that when an array of Object is passed to Arrays.sort, all the members of the array must implement Comparable interface.

Technically, it doesn't matter if actual objects are of different class as long as all those classes implement Comparable (or, to be specific, Comparable<Object>).

In your code, HashTest class does not implement Comparable interface, and hence the ClassCastException.

I hope this helps.


What is causing the actual exception to be thrown to be ClassCastException? Is is trying to convert a HashTest to a String, or vice versa?
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, EVEN IF HashTest DID implement the Comparable interface and overrode the compareTo() function, would that need to be written to compare to a string?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote: What is causing the actual exception to be thrown to be ClassCastException? Is is trying to convert a HashTest to a String, or vice versa?

Actual ClassCastException, as mentioned in your previous error message, is while casting HashTest to Comparable.
Rachel Glenn wrote: Also, EVEN IF HashTest DID implement the Comparable interface and overrode the compareTo() function, would that need to be written to compare to a string?

Now, that's tricky. If your HashTest implements Comparable<Object>, then you must assume that other object (with which current object is being compared to) must be of type Object (which is no big deal), AND, only methods of Object class can be invoked on it.
Of course, you can cast it to HashTest and all, but be prepared for another ClassCastException.

Rule of thumb here is (ok, not exactly a thumb rule):
If you want to sort an array with objects of different classes (e.g. class X and Y), then make sure that:
1) X and Y have common parent (say Z)
2) X and Y implements Comparable<Z>
3) Z holds methods which will matter in compareTo methods of X and Y
4) Create an array of Z and put those objects of X and Y in it

This way, you won't find much trouble while sorting.

Logically, it doesn't make much sense to compare HashTest with String (ever heard of comparing apples with oranges? )

I hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic