Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching Arrays and Collections

 
aslika bahini
Ranch Hand
Posts: 111
  • 0
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • 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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • 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.
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi samura,

you can use exceptions to find out, who calls your method.
 
Harshil Mehta
Ranch Hand
Posts: 64
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic