• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Collections.sort() and reverse()

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody help me explaning ,
difference between answer B and C
Both give right answer according to my unserstanding.
Correct answer is C .
.
Question 189
Given:
ArrayList a = new ArrayList();
containing the values {�1�, �2�, �3�, �4�, �5�, �6�, �7�, �8�}
Which code will return 2?
A. Collections. sort(a, a.reverse());
int result = Collections.binarySearch(a, �6�);
----------------------------------------------------
B. Comparator c = Collections.reverseOrder();
Collections.sort(a, c);
int result = Collections.binarySearch(a, �6�);

C. Comparator c = Collections.reverseOrder();
Collections.sort(a, c);
int result = Collections.binarySearch(a, �6�,c);what is purpose of c here in method arguments
-------------------------------------------------
D. Comparator c = Collections.reverseOrder(a);
Collections.sort(a, c);
int result = Collections.binarySearch(a, �6�,c);
E. Comparator c = new InverseComparator(new Comparator());
Collections.sort(a);
int result = Collections.binarySearch(a, �6�,c);
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucky,



Option A is incorrect since there is no a.reverse() method.


Option D also is incorrect since there is no Collections.reverseOrder(Arraylist a) method.


Option E is incorrect there is no class or interface called InverseComparator and also Comparator class is an abstract class which cannot be instantiated.

Can anybody help me explaning ,
difference between answer B and C





The above code compiles fine , but gives a vague result which cannot be guaranteed.In this case sometimes would be values such as -9,-10 etc.


This code compiles fine and gives the result 2.

You could also have a look at the two binarySearch methods of Collections class inJava API which tells in detail about the differences of the 2 methods.

I hope this helps you.

Thank you
Meera



[ September 24, 2007: Message edited by: Meera Nanda ]
[ September 24, 2007: Message edited by: Meera Nanda ]
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here you are sorting the collection using a Comparator, so you must use the same Comparator in the search method or you wont get accurate results.
 
He does not suffer fools gladly. But this tiny ad does:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic