About Question enthuware.ocpjp.v7.2.1725 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
stobayiwa
Posts: 6
Joined: Thu Dec 31, 2015 1:40 am
Contact:

About Question enthuware.ocpjp.v7.2.1725 :

Post by stobayiwa »

Hie Paul,

I understand that option two on this question is correct since the compare can use the compareTo method to return a result, however i cannot explain what is wrong with option three:
Collections.sort(al, new Comparable<Person>(){     public int compare(Person o1, Person o2) {       return o1.dob.compare(o2.dob);     } });

May u please explain to me why option three is not correct

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1725 :

Post by admin »

Well, as the explanation notes, you need to pass a Comparator as the second argument to the Collections.sort method. While option 3 is passing a Comparable. So it won't compile.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

stobayiwa
Posts: 6
Joined: Thu Dec 31, 2015 1:40 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1725 :

Post by stobayiwa »

Sorry i meant option four wich is passing a Comparator:

Collections.sort(al, new Comparator<Person>(){     
public int compare(Person o1, Person o2) {            
return o1.dob.compare(o2.dob);     } });

I dont seem to get what is wrong with it, may you please explain

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1725 :

Post by admin »

dob is a String and String doesn't have compare method so the line return o1.dob.compare(o2.dob); will not compile.

You may want to try compiling the code.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

stobayiwa
Posts: 6
Joined: Thu Dec 31, 2015 1:40 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1725 :

Post by stobayiwa »

Thank you Paul, I got it now!

Post Reply

Who is online

Users browsing this forum: No registered users and 103 guests