About Question enthuware.ocpjp.v7.2.1307 :
Moderator: admin
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
About Question enthuware.ocpjp.v7.2.1307 :
According to API (http://docs.oracle.com/javase/7/docs/ap ... rator.html), Comparator has 2 methods. But this one only has the implementation of compare(). I understand the equals() of this interface is equal to Object's equals(), but not 100% sure if this class should implement its own equals() as it is defined in the interface.
Last edited by icepeanuts on Sat Feb 08, 2014 2:35 am, edited 1 time in total.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
All classes get equals and hashCode methods from Object class. So it is not necessary to define them in every class.
But yes, it is a good idea to implement both to ensure consistency of equals as well as compare().
HTH,
Paul.
But yes, it is a good idea to implement both to ensure consistency of equals as well as compare().
HTH,
Paul.
-
- Posts: 3
- Joined: Sat Jun 28, 2014 10:29 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Why then doesNote that this guarantees that the return value will be >= 0 if and only if the key is found.
Code: Select all
Arrays.binarySearch(sa, "c", new MyStringComparator());
Answer:
This is because the comparator compares length, not actual values.
Therefore, the position for an item with length one is zero.
Replacing "c" with any one length string will yield the same result e.g. "q", "=", "+"
I hope this saves someone a few minutes

-
- Posts: 15
- Joined: Thu Feb 05, 2015 3:50 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Hello,
I was in doubt about this because sort has not been called before binary search. Maybe it will worth the effort to add this call in the example?
I was in doubt about this because sort has not been called before binary search. Maybe it will worth the effort to add this call in the example?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
It is not necessary to call sort if you know that the list is already sorted.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 15
- Joined: Thu Feb 05, 2015 3:50 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Humm, actually I'm not sure the list is sorted.
Indeed:
String[] sa = { "d", "bbb", "aaaa" };
Arrays.sort(sa);
System.out.format("%s %s %s%n", sa[0],sa[1],sa[2] ); => aaaa bbb d
Did I miss something please?
Indeed:
String[] sa = { "d", "bbb", "aaaa" };
Arrays.sort(sa);
System.out.format("%s %s %s%n", sa[0],sa[1],sa[2] ); => aaaa bbb d
Did I miss something please?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
For the binarySearch method to work, the list has to be sorted as per the Comparator that is being used to search.
-
- Posts: 15
- Joined: Thu Feb 05, 2015 3:50 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Oups, thank you.
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Could you please explain?
I believed that if there no such element in array the return value must be less than 0, but
in this case :
Arrays.binarySearch(sa, "c", new MyStringComparator());
return value is 0.
Yes Comparator compares length of Strings, but shouldn`t it be negative ?
I believed that if there no such element in array the return value must be less than 0, but
in this case :
Arrays.binarySearch(sa, "c", new MyStringComparator());
return value is 0.
Yes Comparator compares length of Strings, but shouldn`t it be negative ?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
You are right. If there is no such element in the array the return value must be less than 0.
But "c" is there in the array sa, as per MyStringComparator.
But "c" is there in the array sa, as per MyStringComparator.
Who is online
Users browsing this forum: No registered users and 2 guests