Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Searching Arrays and Collections  RSS feed

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi samura,

you can use exceptions to find out, who calls your method.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Harshil Mehta:
How come this line prints -1?

Have a look at the API documentation of binarySearch.
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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

Thanks
samura

 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!