• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

K&B BOOK PAGE NUMBER(558)

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These Statements are the Exam watch from K&B Book

When solving searching and sorting questions, two big gotchas are:
1. Searching an array or collection that hasn�t been sorted.
2. Using a Comparator in either the sort or the search,
but not both.


First Statement i have understood.
What is the meaning of the second statement?

i have understood like this.

In a particular program ,if we want to sort an array either we can use Comparator in sort or search method but not both

But the below program is using both

please check at line 1 and line 2
import java.util.*;
class SearchObjArray {
public static void main(String [] args) {
String [] sa = {"one", "two", "three", "four"};
Arrays.sort(sa); // #1
for(String s : sa)
System.out.print(s + " ");
System.out.println("\none = "
+ Arrays.binarySearch(sa,"one")); // #2
System.out.println("now reverse sort");
ReSortComparator rs = new ReSortComparator(); // #3
Arrays.sort(sa,rs);//line 1
for(String s : sa)
System.out.print(s + " ");
System.out.println("\none = "
+ Arrays.binarySearch(sa,"one")); // #4
System.out.println("one = "
+ Arrays.binarySearch(sa,"one",rs)); // line 2
}

Please explain me about this.

Thanks
Anil kumar
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


2. Using a Comparator in either the sort or the search,
but not both.



It does mean: You have used Comparator to sort the array but while searching using binarySearch() method you are not using that comparator. How will binarySearch() method decide in what way your array is sorted.

Suppose your array is sorted in descending order (you have passed Comparator
to the method to sort), and while using binarySearch() you are not telling
the method that the you have used particular Comparator, what will binarySerach() method think? It will do its work according to the default
Comparable way that you have implemented in the class.


The same can happen in case when you sort using different Comparator and search using different Comparator. The result will be unpredictable.




Regards,
cmbhatt
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic