Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sets

 
Neil Muya
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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!
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic