File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Searching Arrays and Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Searching Arrays and Collections" Watch "Searching Arrays and Collections" New topic
Author

Searching Arrays and Collections

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111



my question:

behind the scene we invoke the compare() method. I want to know at which line it is invoked?

line # 4 which takes Arrays.sort(sa,rs)
if, the 2nd argument (rs) is to invoke compare(). then in compare() method we pass 2 arguments String a, String b. what it holds exactly during comparison. and how it is compared to give reverse order?

Thank you
samura
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by samura babu:
...how it is compared to give reverse order? ...

b.compareTo(a) gives the reverse order of a.compareTo(b).

If you want to see where exactly it's invoked, add some println statements, like...

[ June 29, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
By the way, when you use tags you are supposed to put properly formatted code between them. Otherwise people are likely to ignore it, like I did.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi samura,

you can use exceptions to find out, who calls your method.
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64


How come this line prints -1? After sorting the array using ReSortComparator, the array contains elements in the order:
[two three one four]

So the output of the line at #5 should be 2. I am not getting why it prints -1?
[ June 30, 2007: Message edited by: Harshil Mehta ]
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Harshil Mehta:
How come this line prints -1?
Have a look at the API documentation of binarySearch.
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111



can any one explain the above code after # 3. I still have
doubt.

Thanks
samura

Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi samura,

Arrays.sort(sa,rs);
Sorts the list with the comparator. Note, the list is now in descending order.

for(String s : sa)
System.out.print(s + " ");

Prints all elements in the list.

System.out.println("\none = " + Arrays.binarySearch(sa,"one"));
Finds nothing, since the search without comparator expects the list in ascending order.

System.out.println("one = " + Arrays.binarySearch(sa,"one",rs));
Now the element is found, since sort and search use the same comparator.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Manfred Klug wrote
System.out.println("\none = " + Arrays.binarySearch(sa,"one"));
Finds nothing, since the search without comparator expects the list in ascending order.



When you search an Array with Array.binarySearch, it must be sure, that the array has already been sorted.

When it has been sorted using a Comparator, then the same Comparator has to be used for sort() and for binarySearch().
Otherwise the results are undefined.

The same is true for collections.
A not too long tutorial about the latter can be found somewhere in the middle of the Sun tutorial about collections at:
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Enjoy the last sentence of this tut!


Yours,
Bu.
[ July 01, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching Arrays and Collections
 
Similar Threads
Comparator interface
Comparator interface
sorting array
Object of static class
Comparator's compare() method question