• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Sets

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers this is a code from ExamLab just edited a part to make it work.



I know that :
negative if objOne < objTwo
zero if objOne == objTwo
positive if objOne > objTwo

Here's my question:

how does the set arrange the elements?

Because as of now what I just know is:
if you compare the "1st argument to the 2nd argument" in compare method you get an natural order, or you get a duplicate
if you compare the "2nd argument to the 1st argument" in compare method you get an reverse order , or you get a duplicate

In this method does the set insert the 1st element I added comparing to the second element?


and if it gets a "1"?it puts "ob1" to cs[0]?

sorry I am kinda confused.

Thanks in advance!
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neil,

to simplify writing I set a1 = new A(1) and respectively a2, a3.

Now, when cs.add(a1) is performed, the objects a1 and a3 are compared. Actually it doesn't matter if compare(a1,a3) or
compare(a3,a1) is inoked, since in any case the comparison yields a1 < a3. For:

compare(a1,a3) yields -1, meaning a1 < a3.
compare(a3,a1) yields +1, meaning a3 > a1, which is equivalent to a1 < a3.

So after adding a1, the order of cs is {a1,a3}.

When adding a2, the three elements a1, a2, a3 are compared, yielding the order {a1,a2,a3}.

Hope it helps.
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic