File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Please explain the code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please explain the code." Watch "Please explain the code." New topic
Author

Please explain the code.

Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi All,

Can anybody explain me the the code.



Thanks


The important thing is not to stop questioning.Curiosity has its own reason for existing.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The class implements the Comparator interface. So when a Array.sort() method is called, it uses the specified comparator (here the class VLA2) to compare objects during sorting.

The VLA2 class overrides the compare() method of the Comparator interface. This is the method that will be called to compare objects.

Please read the specifications - Comparator:compare(), http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort(T[], java.util.Comparator) and the Comparators section in Object Ordering Tutorials.

I think the compare method written in the code is wrong. According to the specification it should return a negative int / zero or a positive int. But it just assigns the given a's dish value to b and returns it.

And http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#binarySearch(T[], T, java.util.Comparator) is used
to search the given object, using the given comparator in the given array.

I have modified the compare() method and added some System.out.println() and two more objects in the array in the code shown below.
Note that binarySearch() method uses the comparator to find the object.


Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
yes, the compare method is very badly implemented. The following code includes binarySearch(as it is done in Arrays class) gives you more clarity. Includes overriden toString() method




 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please explain the code.