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

PriorityQueue()

 
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
I hope everything is OK...
Such question arose in me about PriorityQueue when i test myself with K&B book: (page-641, question:9)
When i debugged this code i have seen that's why changes 2 places with 4 when add 1 to pg?
So i have not understood this subject evidently well.. Can you explain it please..


I thank at first
Rizvan
----------------------------------------------------------
My SCJP exam date approximately 15.03.2009
 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I corrected mistakes in the top code
Also i changed this code as following:
..................
When debugging and I can explain so: adds 2, then 4, then 1 but I see 2 and 4 changed places (say myself-ok, maybe 4 is greater than 2) but at the end of debugging 6 has been located the most at the end.(it ought to 7) Why?
And can you explain it ( PriorityQueue rules without Comparator) please?

Thanks,
Rizvan
----------------------------------------------------------
My SCJP exam date approximately 15.03.2009
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rizvan I would recommend you to look at the PriorityQueue documentation once and then tell us if you have any doubts...
 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ankit..
But i don't understand clearly.. therefore that write there ....depending on which constructor is used.(Comparable or Comparator)
But i have not used these there.. I used without them.
I don't understand that while adding numbers they sometimes chage places - sometimes doesn't change..
Can you help?

Thanks
Rizvan
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rizvan PriorityQueue always stores objects in sorted order. If you create a PriorityQueue with a Comparator, then it is used to sort the queue otherwise the default sorting is used to sort the objects in the queue. So if the PriorityQueue is not created with a Comparator and the objects added to the PriorityQueue don't implement Comparable, then you'll get a ClassCastException. Basically what I want to say is that whether created with a Comparator or not, PriorityQueue will always keep its elements sorted. Try out these simple programs
1.


2.


3.


4.
 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friend Ankit,

Thanks you very much..
Questions that I gave are easy although.. but forgive me that
a) I am new at Java for 6 months
b) I do not know English excellently
c) I hurry to get the certificate
Up to this day I have met with difficulties in two subject especially(regular expressions(have been explained well) and above mentioned)
So I believe to the Allah (God) and I rely on to myself...

I wish successes too, you..
Sincerely,
Rizvan

PS: You are further my real friend..
My SCJP exam date approximately 15.03.2009
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh so your exam is 3 days away. Best of luck dude . I hope Allah will listen to your prayers and you'll get a good score...
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic